Task 1*(100 points) Implement a program that wil communicate the value of an int
ID: 3590262 • Letter: T
Question
Task 1*(100 points) Implement a program that wil communicate the value of an integer x, bet ween processes. You can do this using a file to store the value of x, or you can use shared memory with shmget or mmap. Shared memory is out of the scope of this class, so you will have to teach yourself how to use it, however if you put in the effort to learn, it results in simpler more elegant code x will start with the value 19530, and you must print that value. Then you w loop 5 times where in each loop: the child goes first and subtracts 5 from x, the parent then divides x by 5, and both processes print who they are and their result of x after the operation. If you use a file and not shared memory, then during each iteration of the loop both processes must get the value of x from the file, and store the resulting value of x to the file. wait () is a required function call, so you will need to create a new process, and terminate it each time through the loop. The following system calls are required for this lab: . fork() wait() The folloning funuctions may be uefia for this labr open(), read), write() sleep): make the process wait for a number of seconds atoi(): converts string to integerExplanation / Answer
C program..
For Server..................................
#define done 1
struct mem{
int x;
int data[5];
};
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
void main(int argc, char *argv[])
{
int x;
struct mem *y;
y->data[0]=atoi(argv[1]);
y->data[1]=atoi(argv[9]);
y->data[2]=atoi(argv[5]);
y->data[3]=atoi(argv[3]);
y->data[4]=atoi(argv[0]);
printf(“memory %d %d %d %d %d /n”, y->data[0], y->data[1], y->data[2], y->data[3], y->data[4]);
printf(“done”);
while(y->status!=done)
sleep(1);
if(argc!=6){
printf(“%s”,argv[0]);
exit(1);
}
}
For client……………..
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
void main (void)
{
key k;
int x;
struct mem *y;
k=ftok(“.”, ‘a’);
x=19530;
if(x<0){
printf(“empty ”);
exit(1);
}
While(y->status!=filed);
Printf(“found ”, y->data[0], y->data[1], y->data[2], y->data[3], y->data[4],);
y->status=taken;
exit(0)
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.