C and Linux Question Here is the old lab 5 they talk about: Redo lab 5, but now
ID: 3813013 • Letter: C
Question
C and Linux Question
Here is the old lab 5 they talk about:
Redo lab 5, but now use only the signal mechanism system calls The kill() system call does not necessarily terminate a process, it is the system call used to send a signal Don't use wait() or pipe(). You must use kill() and pause() system calls. You must create only one child, and allow the processes to run back and forth. The parent can print the iteration since it runs first. You can still read/write from/to a file. A sample of the screen output is listed below. 19530 ITERATION 1 Parent 19525 Child x 3905 ITERATION 2 Parent x 3900 Child x 780 ITERATION 3 Parent 775 Child 155 ITERATION 4 Parent x 150 Child x 30 ITERATION 5 Parent 25 Child 5 output.txtExplanation / Answer
/* ----------------------------------------------------------------- */
/* PROGRAM SharedCounter.c */
/* The main function serves as the parent who requests a shared */
/* memory of an integer, attaches the shared memory to its own */
/* address space, fills it a number from command line, forks */
/* a child , waits until it's child completes it's calculation (x -5), and parent does x/5 */
/* detaches the shared memory, removes the shared memory, and finally*/
/* exits. The child process just displays the content of the shared*/
/* memory. Note that since the child inherits the address space of */
/* the parent in which shared memory has already been attached, the */
/* child does not have to go through all steps of using shared */
/* memory segments. */
/* We iterate 5 times to fork and create the child process */
/*and after its processing process value at Server side*/
/* ----------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
void ChildProcess(int []);
void main(int argc, char *argv[])
{
int ShmID;
int *ShmPTR;
pid_t pid;
int status;
if (argc != 2) {
printf("Use: %s #1 ", argv[0]);
exit(1);
}
ShmID = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666);
if (ShmID < 0) {
printf("*** shmget error (Parent) *** ");
exit(1);
}
printf("Parent has received a shared memory of an integer... ");
ShmPTR = (int *) shmat(ShmID, NULL, 0);
if ((int) ShmPTR == -1) {
printf("*** shmat error (server) *** ");
exit(1);
}
printf("Parent has attached the shared memory... ");
ShmPTR[0] = atoi(argv[1]);
printf("Parent has filled %d in shared memory... ",
ShmPTR[0]);
for (i=0 ; i<5 ; i++) {
printf("Parent is about to fork a child process %d... " , i);
pid = fork();
if (pid < 0) {
printf("*** fork error (Parent) *** ");
exit(1);
}
else if (pid == 0) {
ChildProcess(ShmPTR,i);
exit(0);
}
wait(&status);
printf("Parent has detected the completion of its child... ");
ShmPTR[0] = ShmPTR[0] / 5 ;
printf(" ITERATION %d",i);
printf(" Parent : X = %d in shared memory ", ShmPTR[0])
printf("Parent has removed its shared memory... ");
}
shmdt((void *) ShmPTR);
printf("Parent has detached its shared memory... ");
shmctl(ShmID, IPC_RMID, NULL);
printf("Parent has removed its shared memory... ");
printf("Parent exits... ");
exit(0);
}
void ChildProcess(int SharedMem[],int count)
{
printf(" Child process %d started ",count);
SharedMem[0] -= 5
printf(" ITERATION %d",count);
printf(" Child : X = %d in shared memory ",
SharedMem[0]);
printf(" Child is about to exit ");
exit(0)
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.