Shared memory with forked child - c

I have the following code which is supposed to create a forked process to execute the Collatz conjecture (based on a passed value), and push the integers into shared memory. When the child process is done, the parent process is supposed to print out the values. For some reason, my code works sometimes but not others. From debug statements, I can see the values get pushed in but sometimes the code to print out the values doesn't appear to execute (just some blank lines get printed). I am using Debian Linux in a virtual box.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/mman.h>
int main()
{
const int SIZE = 2048;
const char *name = "SHARON";
int shm_fd;
void *ptr;
int count = 1;
/* Setup shared memory */
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
ftruncate(shm_fd, SIZE);
ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);
/* Get the starting value */
int value;
printf("Enter a positive integer value: ");
scanf("%d", &value);
printf("\n");
if (value < 0)
{
printf("ERROR: Integer value must be positive!");
return 1;
}
/* Fork child process */
pid_t pid;
pid = fork();
fork();
if (pid < 0)
{
fprintf(stderr, "FORK FAILED\n");
return 1;
}
else if (pid > 0) /*parent process*/
{
wait(); /*wait for child to send */
while (atoi((char *)ptr) != 0) /*0 is terminate value*/
{
printf("%s", (char *)ptr);
ptr += sizeof(int);
if (atoi((char *)ptr) != 0)
printf(", ");
}
printf("\n");
shm_unlink(name);
}
else if (pid == 0) /* child process */
{
sprintf(ptr, "%d", value);
ptr += sizeof(value);
while (value != 1)
{
if (value % 2 == 0)
value /= 2;
else
value = value * 3 + 1;
sprintf(ptr, "%d", value);
ptr += sizeof(value);
}
sprintf(ptr,"0"); //push a "terminate" value
ptr += sizeof(value);
}
return 0;
}
Any hints on what I'm doing wrong?

There is two fork() call. which will create two child.
pid = fork();
//fork();
Can you try with above.

Related

How do I print stored data from the shared memory?

I have the following program:
#include <stdio.h>
#include <sys/types.h>
#define MAX_COUNT 100
void ChildProcess(void);
void ParentProcess(void);
void main(void)
{
pid_t pid;
pid = fork();
if (pid == 0)
ChildProcess();
else
ParentProcess();
}
void ChildProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf(" This line is from child, value = %d\n", i);
printf(" *** Child process is done ***\n");
}
void ParentProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value = %d\n", i);
printf("*** Parent is done ***\n");
}
I have to modify it in a way that both the parent and the child print stored data from the shared memory in the following way:
Create and initialize the shared memory in the parent.
Fill the shared memory with 5 integer numbers. (I should allocate enough shared memory to store the 5 ints.)
Fork from the parent to the child.
If fork is successful, then the child process must print the values stored in the shared memory as shown in the expected output where N1, N2, N3, N4, N5 are the numbers found in the shared memory.
Expected output
What I did in the ParentProcess function is the following:
void ParentProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value = %d\n", i);
printf("*** Parent is done ***\n");
int localVar = 0;
int* p = (int*) malloc(2);
pid_t childPID = fork();
*p = 0;
if (childPID >= 0)
{
printf("\nChild process has started\n");
if (childPID == 0)
{
localVar++;
globalVar++;
printf("Child process has found the following data %d,", *p);
*p = 70;
printf( " %d,", *p);
*p = 66;
printf(" %d,", *p);
*p = 51;
printf(" %d,", *p);
*p = 90;
printf(" %d in shared memory\n",*p);
printf("Child is existing\n\n");
}
}
}
And now I realize that I did it completely wrong but I have no idea how to fix that. I suppose I have to use shmget to create the shared memory, but then what? How do I store values in it?
If you find that you cannot help me with this or it is too long, please share sources where I can learn more about C programming in Linux, particularly regarding the usage of shared memory. Thank you in advance
It may be better to make it clear what you want to do first because as far as I read your code you call fork() twice in your code (once in main() function and once in ParentProcess() function)
So I write general solution for parent/child shared memory. There are several ways to achieve shared memory but this is one example which is modified version of the code here
How to use shared memory with Linux in C
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/wait.h>
void *create_shared_memory(size_t size)
{
int protection = PROT_READ | PROT_WRITE;
int visibility = MAP_SHARED | MAP_ANONYMOUS;
return mmap(NULL, size, protection, visibility, -1, 0);
}
int main()
{
// Allocate 4 ints
void *shmem = create_shared_memory(sizeof(int)*4);
if( shmem == NULL ){
fprintf(stderr, "Failed to create shared memory\n");
return -1;
}
// Initialize 4 ints
((int*)shmem)[0] = 10;
((int*)shmem)[1] = 100;
((int*)shmem)[2] = 1000;
((int*)shmem)[3] = 10000;
int pid = fork();
if (pid == 0)
{
// Print 4 ints in child
printf("Child reading int 0: %d\n", ((int*)shmem)[0]);
printf("Child reading int 1: %d\n", ((int*)shmem)[1]);
printf("Child reading int 2: %d\n", ((int*)shmem)[2]);
printf("Child reading int 3: %d\n", ((int*)shmem)[3]);
printf("Child end\n");
}
else
{
printf("Parent waiting for child ends...\n");
waitpid(pid, NULL, 0);
printf("Parent ends\n");
}
int ret = munmap(shmem, sizeof(int)*4);
if( ret != 0 ){
fprintf(stderr, "Failed to unmap shared memory\n");
return -1;
}
return 0;
}
I've written a small piece of c code which you might find helpful:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#define NUM_INTS 5
int main(int argc, char *argv[])
{
key_t key = (key_t) 123456;
int shmgetrc, semgetrc;
struct shmid_ds ds;
int *shared_values;
int i;
struct sembuf sops[2];
int semid;
sops[0].sem_num = 0; /* Operate on semaphore 0 */
sops[0].sem_op = 0; /* Wait for value to equal 0 */
sops[0].sem_flg = 0;
sops[1].sem_num = 0; /* Operate on semaphore 0 */
sops[1].sem_op = 1; /* Increment value by one */
sops[1].sem_flg = 0;
/* create SHM segment */
shmgetrc = shmget(key, NUM_INTS * sizeof(int), IPC_CREAT | IPC_EXCL | 0x180);
if (shmgetrc < 0) {
perror("shmget failed...");
exit(1);
}
/* retrieve the address of the segment */
shared_values = (int *) shmat(shmgetrc, NULL, 0);
/* create a semaphore */
semgetrc = semget(key, 1, IPC_CREAT | IPC_EXCL | 0x180);
if (semgetrc < 0) {
perror("semget failed...");
exit(1);
}
/* lock the semaphore */
if (semop(semgetrc, sops, 2) == -1) {
perror("semop lock failed ...");
exit(1);
}
/* fill it with values */
for (i = 0; i < NUM_INTS; ++i) {
shared_values[i] = i;
}
/* unlock the semaphore */
sops[0].sem_op = -1;
if (semop(semgetrc, sops, 1) == -1) {
perror("semop release failed ...");
exit(1);
}
/* here something else could happen */
sleep(60);
/* lock the semaphore */
sops[0].sem_op = 0;
if (semop(semgetrc, sops, 2) == -1) {
perror("semop lock failed ...");
exit(1);
}
/* print values */
for (i = 0; i < NUM_INTS; ++i) {
printf("%d ", shared_values[i]);
}
printf("\n");
/* unlock the semaphore */
sops[0].sem_op = -1;
if (semop(semgetrc, sops, 1) == -1) {
perror("semop release failed ...");
exit(1);
}
/* remove the semaphore */
if (semctl(semgetrc, semgetrc, IPC_RMID) < 0) {
perror("semctl failed ...");
exit(1);
}
/* remove shm segment again */
if (shmctl(shmgetrc, IPC_RMID, &ds) < 0) {
perror("shmctl failed ...");
exit(1);
}
exit(0);
}
It was not my intention to write the most beautiful code ever written, just an example that shows:
how to create a shm segment
how to retrieve the address and to use it
how to remove it
Additionally, I've used a semaphore to protect the access.
Contrary to the other answer, I've used the ipc interface, not mmap().

shm_open segmentation fault and permission failed

I am new to Linux and i am trying to create a shared memory object which stores the collatz conjecture calculated in child process and prints it in parent process. I have already read the man pages for the commands.
When I create the object it prints permission denied and segmentation fault(core dumped). Only one time it somehow passed that step then I got the mapping failed error.I am using ubuntu 18.04 on a virtual machine
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/mman.h>
#include<sys/wait.h>
#include<sys/stat.h>
int main(int argc,char** argv)
{
pid_t pid;
pid = fork();
int page_size = 4096;
char obj[] = "name";
int num = atoi(argv[1]);
if(num < 1)
{
printf("Please input a greater number\n");
return 0;
}
if(pid < 0)
{
fprintf(stdout,"Fork failed\n");
}
else if(pid == 0)
{
int fd1 = 0;
void *ptr1 = NULL;
fd1 = shm_open(obj,O_CREAT|O_TRUNC,S_IRWXU);
if (fd1 == -1)
{
perror("error:");
exit(0);
}
ftruncate(fd1,page_size);
ptr1 = mmap(0,page_size,PROT_WRITE,MAP_SHARED,fd1,0);
if(ptr1 == MAP_FAILED)
{
fprintf(stdout,"Mapping failed");
exit(0);
}
else
{
while(num != 1)
{
if(num%2 == 0)
num = num / 2;
else
num = (num * 3) + 1;
sprintf(ptr1,"%d, ",num);
}
}
}
else
{
wait(NULL);
int fd = 0;
void *ptr = NULL;
shm_open(obj,O_RDONLY,S_IRWXU);
ptr = mmap(0,page_size,PROT_READ,MAP_SHARED,fd,0);
char *pr = (char *)ptr;
fprintf(stdout,pr);
shm_unlink(obj);
}
return 0;
}
Your code needs a couple of changes, some for better programming practices and some for fixing the actual functionality:
#include <all.h>
int main(int argc, char **argv) {
pid_t pid;
pid = fork();
int page_size = 4096;
char obj[] = "name";
// check argc otherwise you'll segfault
if (argc < 2) {
printf("Please provide a second argument.\n");
// if the program errors it should not return 0
return 1;
}
int num = atoi(argv[1]);
if (num < 1) {
printf("Please input a greater number\n");
return 1;
}
if (pid < 0) {
// no need for fprintf(stdout
printf("Fork failed\n");
} else if (pid == 0) {
int fd1 = 0;
char *ptr1 = NULL;
// we need to open with O_RDWR otherwise mmap will fail
fd1 = shm_open(obj, O_CREAT | O_TRUNC | O_RDWR, 0666);
if (fd1 == -1) {
perror("shm_open error");
return 1;
}
ftruncate(fd1, page_size);
// cast it to char* directly, no need for void pointer stuff
ptr1 = (char *) mmap(0, page_size, PROT_WRITE, MAP_SHARED, fd1, 0);
if (ptr1 == MAP_FAILED) {
// perror for debugging
perror("mmap error");
printf("Mapping failed.\n");
return 1;
} else {
while (num != 1) {
if (num % 2 == 0)
num = num / 2;
else
num = (num * 3) + 1;
// we need to shift the pointer so it stops truncating
ptr1 += sprintf(ptr1, "%d, ", num);
}
}
} else {
wait(NULL);
// move these to one like and cast to char* directly
int fd = shm_open(obj, O_RDONLY, S_IRWXU);
char *ptr = (char*) mmap(0, page_size, PROT_READ, MAP_SHARED, fd, 0);
// no need for fprintf(stdout, and also use %s\n as a format to add a newline
printf("%s\n", ptr);
shm_unlink(obj);
}
return 0;
}

Child process prints on redirected stdout, parent never receives

I'm a beginner when it comes to C and I'm writing this program where I have an array at the beginning and have to fork twice so that I have two child processes. Each child process gets half of the parent's array and executes the program based on it, making it recursive.
When a child gets an array containing only one element, that element is printed on stdout and then it exits.
Each parent then reads what its two children send on stdout and makes a calculation based on it. The result of the calculation is an array that is then being printed also on the stdout for the parent's parent's calculation.
What's not working:
I'm getting to the end of the recursion and printing out the value of the 1-element array in each child, but the parent can't seem to read it.
It never enters the while loop where I'm using fgets().
This is the part in parent where I'm splitting the array in two. Here I'm also giving out the value (without doing anything else) in case I get an array with one value:
while( fgets(strBuffer, sizeof(strBuffer), stdin)!= NULL){
if(counter % 2 == 0){
if(evenCounter == evenMax){
evenMax++;
evenBuffer = (char**) realloc(evenBuffer, evenMax *
sizeof(char*));
}if(evenBuffer == NULL){
fprintf(stderr, "error reallocating evenBuffer\n");
exit(1);
}
evenBuffer[evenCounter] = (char*) malloc(100*sizeof(char));
strcpy(evenBuffer[evenCounter], strBuffer);
evenCounter += 1;
} else {
if(oddCounter == oddMax){
oddMax ++;
oddBuffer = (char**) realloc(oddBuffer, oddMax *
sizeof(char*));
}if(oddBuffer == NULL){
fprintf(stderr, "error reallocating oddBuffer\n");
exit(1);
}
oddBuffer[oddCounter] = (char*) malloc(100*sizeof(char));
strcpy(oddBuffer[oddCounter], strBuffer);
ddCounter += 1;
}
counter ++;
}
if(counter == 1){
fprintf(stdout, "%s\n", evenBuffer[0]);
// fprintf(stderr, "LAAAAST %s\n", evenBuffer[0]);
fflush(stdout);
exit(0);
}
if(oddCounter != evenCounter){
fprintf(stderr, "evenCounter size:%d, oddCounter:%d\n",
evenCounter, oddCounter);
exit(EXIT_FAILURE);
}
Here's the code part where the children are created and the pipes are created and redirected.
int k1pipe_from[2];
int k1pipe_to[2];
pipe(k1pipe_from);
pipe(k1pipe_to);
int k2pipe_from[2];
int k2pipe_to[2];
pipe(k2pipe_from);
pipe(k2pipe_to);
pid_t pid = fork();
pid_t pid2;
switch(pid){
case -1:
fprintf(stderr, "Cannot fork!\n");
exit(EXIT_FAILURE);
case 0://child 1
fprintf(stderr, "Child 1 created, pid: %d\n", getpid());
close(k2pipe_from[1]);
close(k2pipe_to[0]);
close(k1pipe_from[1]);
close(k1pipe_to[0]);
dup2(k1pipe_from[0], STDIN_FILENO);
close(k1pipe_from[0]);
dup2(k1pipe_to[1], STDOUT_FILENO);
close(k1pipe_to[1]);
fflush(stdout);
execl("forkFFT", "forkFFT", NULL);
break;
default:
close(k1pipe_to[1]);
close(k1pipe_from[0]);
fflush(stdout);
}
pid2 = fork();
switch(pid2){
case -1:
fprintf(stderr, "Cannot fork!\n");
exit(EXIT_FAILURE);
case 0://child 2
close(k1pipe_from[1]);
close(k1pipe_to[0]);
close(k2pipe_from[1]);
close(k2pipe_to[0]);
dup2(k2pipe_from[0], STDIN_FILENO);
close(k2pipe_from[0]);
dup2(k2pipe_to[1], STDOUT_FILENO);
close(k2pipe_to[1]);
fflush(stdout);
execl("forkFFT", "forkFFT", NULL);
break;
default:
close(k2pipe_to[1]);
close(k2pipe_from[0]);
}
Here's the part where I'm reading what one of the child processes has written on its redirected stdout.
FILE* k1File = fdopen(k1pipe_to[0], "r+");
char r1Buffer[1000];
evenResCounter = 0;
char* pend1;
while(fgets(r1Buffer, strlen(r1Buffer), k1File) != NULL){
double real = (double) strtof(r1Buffer, &pend1);
double img = 0.00;
if(pend1 != NULL){
img = (double) strtof(pend1, NULL);
}
evenRes[evenResCounter] = real + img * I;
evenResCounter ++;
}
close(k1pipe_to[0]);
Here's the part with the calculation, after which the parent prints out the calculated array:
double pi = 3.141592654;
int total_elem = evenResCounter + oddResCounter;
double complex transArray[total_elem];
int k = 0;
int half = total_elem/2;
while(k <= half){
transArray[k] = evenRes[k] + (cos(-2*pi/total_elem*k) + I * sin(-2*pi/total_elem*k)) * oddRes[k];
transArray[k + half] = evenRes[k] - (cos(-2*pi/total_elem*k) + I * sin(-2*pi/total_elem*k)) * oddRes[k];
k++;
}
int final_counter = 0;
while(final_counter != total_elem){
fprintf(stdout, "%f %f*i\n", creal(transArray[final_counter]), cimag(transArray[final_counter]));
}
Would be super grateful for any help.
You should get pipe for child send message to parent
In child, redirect stdout to pipe, recursive or just output data
In parent, redirect stdin to pipe, get data, if in main process, call calculation(), else just call output_data()
The following code just get the sum of each data, you could change it in calculation():
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int sum = 0;
void output_data(int data) {
printf("%d\n", data);
}
void calculation(int data) {
sum += data;
}
void child(int* array, int left, int right, bool in_main_process) {
if (left == right)
exit(0);
if (left + 1 == right) {
in_main_process ? calculation(array[left]) : output_data(array[left]);
exit(0);
}
int mid = (left + right) / 2;
int pipe_fd_1[2];
pipe(pipe_fd_1);
if (fork() == 0) { // child 1
close(pipe_fd_1[0]);
dup2(pipe_fd_1[1], STDOUT_FILENO);
close(pipe_fd_1[1]);
child(array, left, mid, false);
exit(1);
}
int pipe_fd_2[2];
pipe(pipe_fd_2);
if (fork() == 0) { // child 2
close(pipe_fd_2[0]);
dup2(pipe_fd_2[1], STDOUT_FILENO);
close(pipe_fd_2[1]);
child(array, mid, right, false);
exit(1);
}
close(pipe_fd_1[1]);
dup2(pipe_fd_1[0], STDIN_FILENO);
close(pipe_fd_1[0]);
int data;
while (scanf("%d", &data) == 1)
in_main_process ? calculation(data) : output_data(data);
close(pipe_fd_2[1]);
dup2(pipe_fd_2[0], STDIN_FILENO);
close(pipe_fd_2[0]);
while (scanf("%d", &data) == 1)
in_main_process ? calculation(data) : output_data(data);
}
int main() {
int array[6] = {1, 2, 3, 4, 5, 6};
int left = 0;
int right = sizeof(array) / sizeof(array[0]);
child(array, left, right, true);
printf("%d\n", sum);
return 0;
}

static variables in multiple processes (Signals)

I have 2 processes running test.c. There is a signal handler in test.c which executes an execlp. In test.c, I have a static variable which needs to be only initialized once, and incremented each time before the execlp call. When either process reaches 99, they exit.
Unfortunately, right now, it's not getting incremented, my guess is because there are 2 processes that each have a copy of the static variable. Here is test.c:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
static int i = 0;
static int foo(int j)
{
printf("In the foo...\n");
j++;
printf("%d\n", j);
return j;
}
void main(int argc, char *argv[])
{
int pid, pid2, k;
int *h = malloc(sizeof(int));
int g = 0;
h = &g;
static char s[15];
pid = fork();
if (pid > 0)
{
sleep(1);
}
if (pid == 0)
{
k = foo(*h);
sprintf(s, "%d", k);
if (k >= 99)
{
printf("k=99\n");
exit(0);
}
execlp("./a.out", "forktest", s, NULL);
}
pid2 = fork();
if (pid2 == 0)
{
k = foo(*h);
sprintf(s, "%d", k);
if (k >= 99)
{
printf("k=99\n");
exit(0);
}
execlp("./a.out", "forktest", s, NULL);
}
wait(pid2);
wait(pid);
}
Can anyone please explain why there is an infinite loop? Why isn't the static variable get incremented?
Thank you.
Use Interprocess communication concepts (pipe, fifo, shared memory) here, execlp function overwrites memory of current program with new program. So when ever you call execlp gets called your program get refreshed and starts from begining and static int i is always 0.
I recommend to use pipe Refer this.
You need to use memory projection (mmap function) if you want to use the concept of shared memory between process.
In your code, the variable 'h' is the shared variable between the three process.It should defined using mmap function and initialized in the main process and then incremented in the two child process.
The answers to your two questions are related: either of the two child process never exits (exit(0)) because the if(k>=99) is never statisfied. This is due to the non-shared variable h which doesn't get incremented.
I will rather use a while loop and a return type main function.
By the way, you don't need the 'g' varibale, you can initialize directly 'h'. And there is no need of declaring the function foo as static (static functions are only useful when you want them to visible only with the file where they are defined). The buffer 's' can be declared non static (it is only a buffer which contains the value of k)
Here is a modified version of your code, it compiles and works fine.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
int foo(int* j)
{
printf("In the foo...\n");
(*j)++;
printf("%d\n", *j);
return *j;
}
int main(void)
{
int pid, pid2, k;
char s[15];
int * h = (int*)mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (h == MAP_FAILED) {
printf("map failed\n");
return -1;
}
*h = 0;
pid = fork();
if (pid < 0) {
printf("fork failed pid\n");
return -1;
}
if (pid > 0) {
sleep(1);
}
else {
while(1) {
k = foo(h);
sprintf(s, "%d", k);
if (k>=99) {
printf("k>=99\n");
printf("%s\n", s);
exit(0);
}
execlp("./a.out", "forktest", s, NULL);
}
}
pid2 = fork();
if (pid2 < 0) {
printf("fork failed pid2\n");
}
if (pid2 > 0) {
sleep(1);
}
else {
while(1) {
k = foo(h);
sprintf(s, "%d", k);
if (k>=99) {
printf("k>=99\n");
exit(0);
}
execlp("./a.out", "forktest", s, NULL);
}
}
wait(pid);
wait(pid2);
return 0;
}
Here is the output (only the last strings) click on the link:
output

Forking 3 processes, using shared memory

I have an assignment, and im beating my head against the wall. It is in C. I have a feeling im close to the solution, however I cant get the program to do whats required. I am changing the numbers and some small details, because most of the class is as stumped as I.
Requirements: Create 3 processes, the first one will increment a shared memory variable "total->value" from 1 to 10000, the second from 10000 to 12000, the third from 12000 to 14000
The process functions are labeled such (process1(), process2(), process3())
and the internals of those functions are as follows
process1()
{
int k = 0;
while (k < 10000)
{
k++;
total->value = total->value + 1;
}
printf("From process 1 = %d/n", total->value);
}
The second would be k < 2000 (because it only needs to increment the shared value 2000 more) and etc.
The main portion of the program is:
main()
{
int shmid;
int pid1;
int pid2;
int pid3;
int ID;
int status;
char *shmadd = (char *)0;
/* Create and connect to a shared memory segmentt */
if ((shmid = shmget(SHMKEY, sizeof (int), IPC_CREAT | 0666)) < 0)
{
perror("shmget");
exit(1);
}
if ((total = (shared_mem *)shmat(shmid, shmadd, 0)) == (shared_mem *)-1)
{
perror("shmat");
exit(0);
}
total->value = 0;
if ((pid1 = fork()) == 0)
process1();
if ((pid1 != 0) && (pid2 = fork()) == 0)
process2();
if ((pid1 != 0) && (pid2 != 0) && (pid3 = fork()) == 0)
process3();
if ((pid1 != 0) && (pid2 != 0) && (pid3 != 0))
{
if ((shmctl(shmid, IPC_RMID, (struct shmid_ds *)0)) == -1)
{
perror("shmctl");
exit(-1);
}
printf("\t\t End of Program.\n");
}
}
What I need is for the first process to finish, before the 2nd starts. I tried inserting a wait(&status) after the process1() (or 2 or 3) calls and am at a loss. Any pointers? (no pun intended) =) there is more to implement, but I believe once I have this part I can handle the rest on my own. I have been intentionally vague in some regards, but I would like to finish this project and more importantly understand it and there are others who want a free lunch. I will provide anything else in the code that is required. Thank you in advance for your help
The output should appear
From process 1 = 10000
From process 2 = 12000
From process 3 = 14000
I believe that Celada's comment/guess on the requirements is correct. However, barring that, and at the risk of doing too much work, the following code fulfills your spec. The use of the gcc built-in __sync_fetch_and_add() is perhaps unnecessary.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/wait.h>
static struct {
int value;
} *total;
static void process1(void) {
int k = 0;
while (k < 10000) {
k++;
__sync_fetch_and_add(&total->value, 1);
}
printf("From process 1 = %d\n", total->value); //<-- not quite right: could be >10000
}
static void process2(void) {
int k = 0;
while (__sync_fetch_and_add(&total->value, 0) != 10000)
;
while (k < 2000) {
k++;
__sync_fetch_and_add(&total->value, 1);
}
printf("From process 2 = %d\n", total->value);
}
static void process3(void) {
int k = 0;
while (__sync_fetch_and_add(&total->value, 0) != 12000)
;
while (k < 2000) {
k++;
__sync_fetch_and_add(&total->value, 1);
}
printf("From process 3 = %d\n", total->value);
}
int main(void) {
int shmid;
int pid1;
int pid2;
int pid3;
int status;
/* Create and connect to a shared memory segment */
if ((shmid = shmget(1234, sizeof *total, IPC_CREAT|0666)) < 0) {
perror ("shmget");
exit (1);
}
if ((total = shmat(shmid, 0, 0)) == (void *)-1) {
perror("shmat");
exit (0);
}
total->value = 0; // not necessary in Linux if IPC_CREAT invoked
if (!(pid1 = fork()))
process1();
else if (!(pid2 = fork()))
process2();
else if (!(pid3 = fork()))
process3();
else {
wait(&status);
wait(&status);
wait(&status);
if ((shmctl(shmid, IPC_RMID, (struct shmid_ds *) 0)) == -1) {
perror("shmctl");
exit (-1);
}
printf("\t\t End of Program.\n");
}
return 0;
}

Resources