In: Computer Science
Giva details on explanation too (I.e. NOT just basic knowledge of os)
Can you please build a code for part1 to part8?
- After child is forked and process is created, both parent and
child are attached with shared memory segment on their address in
part1, part2.
- Each will go on for 10 iterations to access the shared memory as
follows-> on i-th iteration (0<=i<10), the parent will
first update the call of a[i]= i.
- And let child read the updated value by reading b[I].
- Use SIGSTOP and SIGCONT signals to make sure whenever the parent
updates one entry in the shared memory the child should read the
updated value afterwards before parent and child move on to next
iteration.
Functions to add:
1. Creating Shared memory
2. Attaching shared memory
3. De-attach shared memory
System call&library allowed : kill(), getpud(), wait(), exit(), shmctl()
-------------------Sample code here------------------
int main(){
pid_t pid;
int shmid, status;
int *a, *b, i;
/*part1: Initialization*/
pid = fork();
if (pid==0){ //child
/*part2: Child process
before entering for loop*/
for (I=0;i<10;i++){
/*part3: Implement correct code
leading right output*/
printf("\t Child
iterating %d time, reading b[%d] = %d.\n", I, I, b[I]);
}
/*part4: Implement
code here*/
}
else{ //parent
/*part5: Parent's
process before for-loop*/
sleep(1);
for (I=0;i<10;i++){
/*part6: implement your code
here*/
a[I] = I;
printf("Parent
iteratin %d time, writing a[%d] = %d.\n", I, I, a[I]);
/*part7:
Implement code here*/
}
/*part8: Add code before the end of the
program*/
}
-------------------------------------------------------------------------
Expected output:
When you run ./main
Parent iterating 0 time, writing a[0] = 0
Child iterating 0 time, reading b[0] = 0
Parent iterating 0 time, writing a[1] = 1
Child iterating 0 time, reading b[1] = 1
Parent iterating 0 time, writing a[2] = 2
Child iterating 0 time, reading b[2] = 2
Parent iterating 0 time, writing a[3] = 3
Child iterating 0 time, reading b[3] = 3
Parent iterating 0 time, writing a[4] = 4
Child iterating 0 time, reading b[4] = 4
Parent iterating 0 time, writing a[5] = 5
Child iterating 0 time, reading b[5] = 5
Parent iterating 0 time, writing a[6] = 6
Child iterating 0 time, reading b[6] = 6
Parent iterating 0 time, writing a[7] = 7
Child iterating 0 time, reading b[7] = 7
Parent iterating 0 time, writing a[8] = 8
Child iterating 0 time, reading b[8] = 8
Parent iterating 0 time, writing a[9] = 9
Child iterating 0 time, reading b[9] = 9
(I.e. Direct explanations not basic knowledge of os)
Can you please build a code for part1 to part8?
After child is forked and process is created, both parent and
child are attached with shared memory segment on their address in
part1, part2.
Each will go on for 10 iterations to access the shared memory as
follows-> on i-th iteration (0<=i<10), the parent will
first update the call of a[i]= i.
And let child read the updated value by reading b[I].
Use SIGSTOP and SIGCONT signals to make sure whenever the parent
updates one entry in the shared memory the child should read the
updated value afterwards before parent and child move on to next
iteration.
Functions to add:
1. Creating Shared memory
2. Attaching shared memory
3. De-attach shared memory
System call&library allowed : kill(), getpud(), wait(), exit(), shmctl()
-------------------Sample code here------------------
int main(){
pid_t pid;
int shmid, status;
int *a, *b, i;
/*part1: Initialization*/
pid = fork();
if (pid==0){ //child
/*part2: Child process
before entering for loop*/
for (I=0;i<10;i++){
/*part3: Implement correct code
leading right output*/
printf("\t Child
iterating %d time, reading b[%d] = %d.\n", I, I, b[I]);
}
/*part4: Implement
code here*/
}
else{ //parent
/*part5: Parent's
process before for-loop*/
sleep(1);
for (I=0;i<10;i++){
/*part6: implement your code
here*/
a[I] = I;
printf("Parent
iteratin %d time, writing a[%d] = %d.\n", I, I, a[I]);
/*part7:
Implement code here*/
}
/*part8: Add code before the end of the
program*/
}
Expected output:
When you run ./main
Parent iterating 0 time, writing a[0] = 0
Child iterating 0 time, reading b[0] = 0
Parent iterating 0 time, writing a[1] = 1
Child iterating 0 time, reading b[1] = 1
Parent iterating 0 time, writing a[2] = 2
Child iterating 0 time, reading b[2] = 2
Parent iterating 0 time, writing a[3] = 3
Child iterating 0 time, reading b[3] = 3
Parent iterating 0 time, writing a[4] = 4
Child iterating 0 time, reading b[4] = 4
Parent iterating 0 time, writing a[5] = 5
Child iterating 0 time, reading b[5] = 5
Parent iterating 0 time, writing a[6] = 6
Child iterating 0 time, reading b[6] = 6
Parent iterating 0 time, writing a[7] = 7
Child iterating 0 time, reading b[7] = 7
Parent iterating 0 time, writing a[8] = 8
Child iterating 0 time, reading b[8] = 8
Parent iterating 0 time, writing a[9] = 9
Child iterating 0 time, reading b[9] = 9
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include<unistd.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<signal.h>
#include<sys/types.h>
int main(){
pid_t pid;
int shmid, status;
int *a, *b, i;
shmid = shmget(IPC_PRIVATE,10*sizeof(int),0777|IPC_CREAT);
/*part1: initialization*/
pid = fork();
if (pid==0){ //child
b = (int *) shmat(shmid, 0, 0);
/*part2: Child process before entering for loop*/
for (i=0;i<10;i++){
kill(getpid(),SIGSTOP);
/*part3: implement correct code leading right output*/
printf("\t Child iterating %d time, reading b[%d] = %d.\n", i, i, b[i]);
kill(getppid(),SIGCONT);
}
shmdt(b);
/*part4: implement code here*/
}
else{ //parent
/*part5: Parent's process before for-loop*/
// sleep(1);
a = (int *) shmat(shmid, 0, 0);
for (i=0;i<10;i++){
/*part6: implement your code here*/
if(i>0)
kill(getpid(),SIGSTOP);
a[i] = i;
printf(" \tParent iteratin %d time, writing a[%d] = %d.\n", i, i, a[i]);
kill(pid,SIGCONT) ;
/*part7: implement code here*/
}
shmdt(a);
/*part8: Add code before the end of the program*/
}
}