static variables in multiple processes (Signals) - c

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

Related

Semaphore and alternating printing

I want the following: first the parent process prints "hello", then the child process prints "world" and this is done 3 times. When I execute my code I only get hello world once but I want to get it printed 3 times.
#include <semaphore.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
int main(int argc, char*argv[]){
char* parent="hello ";
char* child="world";
sem_t *sem = sem_open("/mysem", O_CREAT, 0644, 1);
sem_t *sem1=sem_open("/mysem1", O_CREAT, 0644, 0);
int pid=fork();
if(pid<0){
printf("error in forking");
return -1;
}
int cnt=0, cnt1=0;
if(pid==0){
sem_wait(sem1);
write(1, child, strlen(child));
cnt++;
if(cnt1<3){
sem_post(sem);
}
}else{
sem_wait(sem);
write(1, parent, strlen(parent));
cnt++;
if(cnt<3){
sem_post(sem1);
}
}
return 0;
}
maybe try this :
#include <semaphore.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#define BUFFER_SIZE 7
int main(int argc, char **argv)
{
char parent[BUFFER_SIZE] = "hello ";
char child[BUFFER_SIZE] = "world\n";
sem_t *sem = sem_open("/mysem", O_CREAT, 0644, 1);
sem_t *sem1 = sem_open("/mysem1", O_CREAT, 0644, 0);
int cnt = 0;
int pid = fork();
if (sem == SEM_FAILED || sem1 == SEM_FAILED) {
printf("Error : sem_open() call failed.\n");
return (-1);
}
if (pid < 0) {
printf("error in forking");
return (-1);
}
if (pid == 0) {
while (cnt < 3) {
sem_wait(sem1);
if (write(1, child, strlen(child)) == -1) {
printf("Error : write call failed.\n");
}
cnt++;
sem_post(sem);
}
}
else {
while (cnt < 3) {
sem_wait(sem);
if (write(1, parent, strlen(parent)) == -1) {
printf("Error : write function failed.\n");
}
cnt++;
sem_post(sem1);
}
}
sem_close(sem);
sem_close(sem1);
return (0);
}
Explanation :
#define BUFFER_SIZE 7 this is a macro, 7 because of part 2.
Memory leaks (use valgrind)
Don't do : char *str = "abc";
but do : char str[string_size + 1] = "abc";
Because char *str = doesn't allocate memory so you will probably get memory leaks, always allocate string_size + 1 with C String. You can use malloc too to allocate your own size.
Put your int cnt variables before the fork(), fork will copy all memory so you can put only one variable before your fork call, for more readable code.
You should always call sem_close() as soon as possible and you should always look for returned value from functions that are not your, use C man, for exmaple "man 2 write" in your linux console (not looking for sem_wait semp_post and sem_close returned value shouldn't be problematic here, but you can still print a message if you want).
Your main problem here is that you should switch your if (cnt < 3) into a loop as i did with while, your parent and child process only course once write() if you don't set a loop.
The most important thing, please look for coding style rules to make your code more redeable.
This is pretty long, but you should take at look in all i said.
Compilation :
gcc file.c -pthread
./a.out
I'm on linux, fedora32.
Output :
hello world
hello world
hello world

System call how to make parent wait for child

This is my code system call in C.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int n;
int i;
pid_t pid;
int time = 1000;
int sum = 0;
int main(void) {
printf("n: ");
scanf("%d", &n);
pid = fork();
if (pid < 0) {
printf("Fork Failed");
exit(-1);
} else if (pid == 0) {
//child
for (i = 1; i <= n; i++) {
sum += i;
}
printf("Sum of 1 to %d: %d\n", n, sum); // this is ok
} else {
// parent
wait(&time);
printf("Sum of 1 to %d: %d\n", n, sum); // this always return 0;
}
return 0;
}
I don't know why in parent's code block, the sum is always equal to 0.
How to make parent wait for child or am I doing something wrong ?
Waiting for the child works. However, your expectations are wrong.
Apparently you think that computations in the child process after the fork are visible in the parent process. They are not. The child is a new copy of the parent program at the time of fork. At that time, the parent's sum is 0 and stays that way.
There are several mechanisms to pass data from child to parent (the search term is interprocess communication, IPC).
exit() status
files
shared memory
pipes
signals
message queues
anything else I have missed
The issue here is the variable sum is not shared by the parent & child process, after fork() call the child will have its own copy of the variable sum.
Use shmget(),shmat() from POSIX api. Or use pthread which will share the same memory space for the newly created thread.
Update---
Added the shared memory to your code hopes this helps.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/shm.h>
int n;
int i;
pid_t pid;
int time = 1000;
int main(void) {
int shmid;
int *sum;
printf("n: ");
scanf("%d", &n);
/*request the shared memory from the OS using the shmget()*/
shmid = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT);
pid = fork();
if (pid < 0) {
printf("Fork Failed");
exit(-1);
} else if (pid == 0) {
//child
/* shmat() returns a char pointer which is typecast here
to int and the address is stored in the int pointer. */
sum = (int *) shmat(shmid, 0, 0);
for (i = 1; i <= n; i++) {
*sum += i;
}
printf("Sum of 1 to %d: %d\n", n, *sum); // this is ok
/* each process should "detach" itself from the
shared memory after it is used */
shmdt(sum);
} else {
// parent
wait(&time);
sum = (int *) shmat(shmid, 0, 0);
printf("Sum of 1 to %d: %d\n", n, *sum); // this always return 0;
shmdt(sum);
/*delete the cretaed shared memory*/
shmctl(shmid, IPC_RMID, 0);
}
return 0;
}
Refer for more info- https://man7.org/linux/man-pages/man2/shmget.2.html

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().

Make fork execute equal amounts of times, infinitely

I am trying to make the following code never allow the amount of + to be greater than the amounts of - . I am thinking about adding a simple line of just c sleep(1) but I was curious to see if there is a better way to make the amounts equal. This is supposed to stay an infinite loop.
int main(){
if(fork() ==0)
while(1){
write(1,"-",1)
}
else
while(1){
write(1, "+",1);
}
return 0;
}
Would this function correctly using semaphores?
int main(){
int parent_sem = get_semaphore(0);
int child_sem = get_semaphore(1);
if(fork() ==0)
while(1){
sem_wait(child_sem);
write(1,"-",1);
sem_signal(parent_sem);
}
else
while(1){
sem_wait(parent_sem);
write(1, "+",1);
sem_signal(child_sem);
}
return 0;
}
Here's a fixed version of your semaphore example. I have allocated shared memory so that the semaphores can be accessed by both processes across the fork (I forgot this was necessary, sorry) and fixed the semaphore types and calls.
#include <unistd.h>
#include <semaphore.h>
#include <sys/mman.h>
int main() {
// Create shared memory for two semaphores
sem_t* sem = mmap(NULL, 2 * sizeof(sem_t), PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
// Initialise parent and child semaphores: parent = 1, child = 0
sem_t* parent_sem = sem;
sem_t* child_sem = sem + 1;
sem_init(parent_sem, 1, 1);
sem_init(child_sem, 1, 0);
if (fork() == 0) {
while(1) {
// Child process
sem_wait(child_sem);
write(1, "-", 1);
sem_post(parent_sem);
}
} else {
while(1) {
// Parent process
sem_wait(parent_sem);
write(1, "+", 1);
sem_post(child_sem);
}
}
return 0;
}
This is close (Use signals):
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
void usr1handler() {
}
int main(void) {
int pid;
signal(SIGUSR1, usr1handler);
if ((pid = fork()) == 0) {
while (1) {
pause();
write(1, "-", 1);
fflush(stdout);
kill(getppid(), SIGUSR1);
}
}
else {
sleep(1);
while (1) {
write(1, "+", 1);
fflush(stdout);
kill(pid, SIGUSR1);
pause();
}
}
}
The main issue for this is the race condition. If a process makes it to the kill() function before the other makes it to the pause(), the program freezes. This is probably good enough to start you off though. I left a single sleep in there so you could see that it does indeed print evenly.

Shmget is returning a value of -1

When I run shmget in the following code it is returning a value of -1 and im not sure why that is the case. Everything else seems to be running fine. The code is just supposed to take in a few digits from the command line and then create shared memory for them. The digits will range from 0 to 9.
#include <sys/ipc.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <sys/types.h>
int main(int argc, char *argv[])
{
int numArgc =(int)argc-1; //number of vauled arguments passed
int arrayId[numArgc];
pid_t pid;
int arrSpace[numArgc]; //array to store atoi of values
int status;
int *memory; //pointer to shared memory
int memoryId; //check for smhget
int childPID;
int childId;
if(argc > 8 || argc < 2) //check number of cmd line arg
{
printf("The number of arguments must be between 1 and 7");
return(0);
}
else
{
for(int i=1; i<numArgc+1; i++) //store args as integers
{
arrSpace[i]=atoi((argv[i]));
printf("%d\n", arrSpace[i]);
}
}
memoryId=shmget(IPC_PRIVATE, try, IPC_CREAT | 07546); //create shared
printf("%d \n", memoryId);
if(memoryId<0)
{
printf("There was an error with ID.\n");
return (0);
}
printf("%s%d", "Size of shared Memory of parent is \n ", numArgc);
memory=(int*)shmat(memoryId, NULL, 0); // attaches shared memory
if((long)memory == -1)
{
printf("There was an error running shmat .\n");
return (0);
}
printf("Share memory is now: \n");
for(int i=0; i<numArgc; i++)
{
memory[i]=arrSpace[i];
}
printMemory(memory, numArgc);
printf("Beginning fork process");
for(childId; childId <= numArgc; childId++)
{
pid = fork(); //creates new process
if(pid < 0)
{
printf("There was an error during fork process");
return (0);
}
else if(pid == 0)
{
ChildProcess(memory, numArgc, childId);
exit(0);
}
}
ParentProcess(memory, childPID, numArgc, memoryId, status);
return (0);
}
shmget returns -1 because try variable is undefined and the permission 07546 is invalid.Please pass appropriate permission for the memory segment.
#define MEMORY_SIZE 20 //size of memory segment
memoryId=shmget(IPC_PRIVATE, MEMORY_SIZE , IPC_CREAT | 0666); //create shared
printf("%d \n", memoryId);

Resources