-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprodcon.c
More file actions
169 lines (128 loc) · 4.75 KB
/
Copy pathprodcon.c
File metadata and controls
169 lines (128 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/************************************
Name: Manu Khandelwal
Student #ID: 5272109
CSE Lab Login: khand055
Assignment No: 1-PartB
**************************************/
#include <stdlib.h> /* for random integer */
#include <stdio.h> /* for standard I/O */
#include <unistd.h> /* for fork */
#include <sys/types.h> /*pid_t defined*/
#include <signal.h> /* for Signal Handling */
#include <sys/shm.h> /* For Shared Memory */
#include <sys/wait.h>
#define BUFFER_SIZE 1024
void signal_handler(int);
FILE *fps;
FILE *fpd;
char *source_filename;//="source-file";
char *destination_filename = "destination-file";
int pid_parent, pid_child;
int shmem_id_buffer, shmem_id_count; /* shared memory identifier for buffer and for count */
int *shmem_ptr_buffer, *shmem_ptr_count; /* pointer to shared segment */
key_t key_buffer = 4455, key_count = 4466; /* A key to access shared memory segments */
int flag = 1023, status; /* Controls things like r/w permissions */
int main(int argc, char *argv[]) {
if( argc <=1 ){
printf("Please provide the file name\n");
exit(0);
}
source_filename = argv[1];
if( signal( SIGUSR1, signal_handler) == SIG_ERR ) {
printf("Parent: Unable to create handler for SIGUSR1\n");
}
if( signal( SIGUSR2, signal_handler) == SIG_ERR ) {
printf("Child: Unable to create handler for SIGUSR2\n");
}
/* Creating Shared memory segment for the buffer data*/
shmem_id_buffer = shmget (key_buffer, sizeof(char)*BUFFER_SIZE, flag);
if (shmem_id_buffer == -1)
{
perror ("shmget failed");
exit (1);
}
/* Creating sharede memory segment for the count */
shmem_ptr_buffer = shmat (shmem_id_buffer, (void *) NULL, flag);
if (shmem_ptr_buffer == (void *) -1)
{
perror ("shmat failed");
exit (2);
}
/* Attaching the shared memory segment for buffer to address space */
shmem_id_count = shmget (key_count, sizeof(int), flag);
if (shmem_id_count == -1)
{
perror ("shmget failed");
exit (1);
}
/* Attaching the shared memory segment for count to address space */
shmem_ptr_count = shmat (shmem_id_count, (void *) NULL, flag);
if (shmem_ptr_count == (void *) -1)
{
perror ("shmat failed");
exit (2);
}
printf( "Parent pid = %d\n", pid_parent=getpid() );
remove(destination_filename);
if( (pid_child = fork()) == 0 ) {
printf("Child process started\n" );
pid_child = getpid();
for( ;; );
} else if ( pid_child > 0 ) { // Parent process
printf("Parent (%d) process started\n", pid_parent);
fps=fopen(source_filename, "rb");
shmem_ptr_count[0] = fread(shmem_ptr_buffer, 1, BUFFER_SIZE, fps);
printf("Parent (%d) is sending signal to child (%d) to start copying %d characters\n", pid_parent, pid_child, shmem_ptr_count[0]);
kill( pid_child, SIGUSR2 );
//for( ;; );
waitpid(pid_child, &status, 0);
if( WIFEXITED( status ) ) {
printf("Parent is exiting !! \n");
fclose(fps);
exit(0);
}
} else {
// fork failed
printf("fork() failed!\n");
return 1;
}
printf("Control has come to the very end\n");
}
void signal_handler(int signo)
{
/* signo contains the signal number that was received */
switch( signo )
{
/* Signal is a SIGUSR1 */
case SIGUSR1:
if( pid_parent == getpid() ) { /* it is the parent */
printf("\nParent (%d) has received the signal from the child \n", pid_parent );
if( feof(fps) ){
shmem_ptr_count[0] = EOF;
} else {
shmem_ptr_count[0] = fread(shmem_ptr_buffer, 1, BUFFER_SIZE, fps);
}
kill( pid_child, SIGUSR2 );
}
break;
/* Signal is a SIGUSR2 */
case SIGUSR2:
if( pid_parent != getpid() ) { // It is a child process
if( shmem_ptr_count[0] == EOF ) {
printf("Child is exiting !! \n");
exit(0);
//kill( pid_parent, SIGUSR2 );
}
printf("Child (%d) has received the signal from the parent (%d) and started to copy %d characters \n", getpid(), pid_parent, shmem_ptr_count[0] );
fpd=fopen(destination_filename, "ab");
fwrite (shmem_ptr_buffer , sizeof(char), shmem_ptr_count[0], fpd);
fclose(fpd);
printf("\nChild (%d) sending signal to parent (%d) about finished task copying %d characters\n", getpid(), pid_parent, shmem_ptr_count[0]);
shmem_ptr_count[0] = 0;
kill( pid_parent, SIGUSR1 );
}
break;
defaut:
break;
}
}