Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

40%
+2 −4
Q&A Child process works only once after the parent's two calls to scanf

This program creates a child process and shares two integers (base and height) through the shared memory. The parent process asks four times to insert two integers and wait for the child process t...

1 answer  ·  posted 2y ago by Luca_Impellizzeri‭  ·  last activity 2y ago by Alexei‭

#4: Post edited by user avatar Dirk Herrmann‭ · 2022-05-17T04:48:40Z (almost 2 years ago)
Less radical change proposal, avoiding assumptions about OPs intentions
  • The pid works only once after the two calls to scanf
  • Child process works only once after the parent's two calls to scanf
  • This program creates a pid and shares two integers (`base` and `height`) through the shared memory.
  • The parent process time asks four times to insert two integers and wait for the pid to calculate the area.
  • After the four cycles, the ppid waits for the end of its child, deletes the shared memory and the semaphore and ends. The child waits for the new variable (`base`, `height` with semaphore), calculates the area, and prints it, and after the four iterations, it ends.
  • I have to use semaphore for process synchronization and to regulate the critical section. The problem consists of the pid, it works only one time.
  • ```c
  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <time.h>
  • #include <unistd.h>
  • #include <sys/wait.h>
  • #include <sys/types.h>
  • #include <sys/ipc.h>
  • #include <sys/sem.h>
  • #include <sys/shm.h>
  • #include <string.h>
  • #define SEM_KEY (key_t)8765
  • #define SHM_KEY (key_t)9876
  • /** Questa union la trovate digitando "man semctl" */
  • union semun
  • {
  • int val; /* Value for SETVAL */
  • struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
  • unsigned short *array; /* Array for GETALL, SETALL */
  • struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */
  • };
  • /** Tipo di dato condiviso */
  • struct sh_data
  • {
  • int altezza;
  • int base;
  • };
  • /** Funzioni di appoggio per operare con i semafori */
  • int sem_set(int semid, int val);
  • int sem_down(int semid);
  • int sem_up(int semid);
  • void controlla_area(int, struct sh_data *);
  • int main()
  • {
  • pid_t pid;
  • int semid, shmid, i;
  • struct sh_data *data;
  • // SEMAFORO
  • // Creazione del semaforo
  • semid = semget(SEM_KEY, 1, 0666 | IPC_CREAT);
  • // Inizializzazione semaforo
  • sem_set(semid, 1);
  • // SHARED MEMORY
  • // Creazione shared memory
  • shmid = shmget(SHM_KEY, sizeof(struct sh_data), 0666 | IPC_CREAT);
  • // Attach della shm allo spazio di indirizzi del processo
  • data = (struct sh_data *)shmat(shmid, NULL, 0);
  • // Processo figlio
  • pid = fork();
  • switch (pid)
  • {
  • case -1:
  • perror("Errore fork");
  • exit(EXIT_FAILURE);
  • case 0:
  • controlla_area(semid, data);
  • exit(EXIT_SUCCESS);
  • default:
  • break;
  • }
  • // Processo padre
  • for (i = 0; i < 4; i++)
  • {
  • sem_down(semid);
  • printf("Inserisci la base: ");
  • scanf("%d", &(data->base));
  • printf("Inserisci l'altezza: ");
  • scanf("%d", &(data->altezza));
  • sem_up(semid);
  • wait(NULL);
  • }
  • // Detach della shared memory
  • shmdt(data);
  • // Eliminazione della shared memory
  • shmctl(shmid, IPC_RMID, NULL);
  • // Eliminazione semaforo
  • semctl(semid, 0, IPC_RMID);
  • exit(EXIT_SUCCESS);
  • }
  • void controlla_area(int semid, struct sh_data *data)
  • {
  • int area;
  • sem_down(semid);
  • area = data->base * data->altezza;
  • printf("L'area del triangolo è %d\n", area);
  • sem_up(semid);
  • }
  • int sem_set(int semid, int val)
  • {
  • union semun s;
  • s.val = val;
  • /* Inizializza il valore del semaforo */
  • return semctl(semid, 0, SETVAL, s);
  • }
  • int sem_down(int semid)
  • {
  • struct sembuf buff;
  • buff.sem_num = 0;
  • buff.sem_op = -1;
  • buff.sem_flg = SEM_UNDO;
  • /* Decrementa il valore del semaforo */
  • return semop(semid, &buff, 1);
  • }
  • int sem_up(int semid)
  • {
  • struct sembuf buff;
  • buff.sem_num = 0;
  • buff.sem_op = 1;
  • buff.sem_flg = SEM_UNDO;
  • /* Incrementa il valore del semaforo */
  • return semop(semid, &buff, 1);
  • }
  • ```
  • This program creates a child process and shares two integers (`base` and `height`) through the shared memory.
  • The parent process asks four times to insert two integers and wait for the child process to calculate the area.
  • After the four cycles, the parent process waits for the end of its child, deletes the shared memory and the semaphore and ends. The child waits for the new variable (`base`, `height` with semaphore), calculates the area, and prints it, and after the four iterations, it ends.
  • I have to use semaphore for process synchronization and to regulate the critical section. The problem relates to the child process, it works only one time.
  • ```c
  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <time.h>
  • #include <unistd.h>
  • #include <sys/wait.h>
  • #include <sys/types.h>
  • #include <sys/ipc.h>
  • #include <sys/sem.h>
  • #include <sys/shm.h>
  • #include <string.h>
  • #define SEM_KEY (key_t)8765
  • #define SHM_KEY (key_t)9876
  • /** Questa union la trovate digitando "man semctl" */
  • union semun
  • {
  • int val; /* Value for SETVAL */
  • struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
  • unsigned short *array; /* Array for GETALL, SETALL */
  • struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */
  • };
  • /** Tipo di dato condiviso */
  • struct sh_data
  • {
  • int altezza;
  • int base;
  • };
  • /** Funzioni di appoggio per operare con i semafori */
  • int sem_set(int semid, int val);
  • int sem_down(int semid);
  • int sem_up(int semid);
  • void controlla_area(int, struct sh_data *);
  • int main()
  • {
  • pid_t pid;
  • int semid, shmid, i;
  • struct sh_data *data;
  • // SEMAFORO
  • // Creazione del semaforo
  • semid = semget(SEM_KEY, 1, 0666 | IPC_CREAT);
  • // Inizializzazione semaforo
  • sem_set(semid, 1);
  • // SHARED MEMORY
  • // Creazione shared memory
  • shmid = shmget(SHM_KEY, sizeof(struct sh_data), 0666 | IPC_CREAT);
  • // Attach della shm allo spazio di indirizzi del processo
  • data = (struct sh_data *)shmat(shmid, NULL, 0);
  • // Processo figlio
  • pid = fork();
  • switch (pid)
  • {
  • case -1:
  • perror("Errore fork");
  • exit(EXIT_FAILURE);
  • case 0:
  • controlla_area(semid, data);
  • exit(EXIT_SUCCESS);
  • default:
  • break;
  • }
  • // Processo padre
  • for (i = 0; i < 4; i++)
  • {
  • sem_down(semid);
  • printf("Inserisci la base: ");
  • scanf("%d", &(data->base));
  • printf("Inserisci l'altezza: ");
  • scanf("%d", &(data->altezza));
  • sem_up(semid);
  • wait(NULL);
  • }
  • // Detach della shared memory
  • shmdt(data);
  • // Eliminazione della shared memory
  • shmctl(shmid, IPC_RMID, NULL);
  • // Eliminazione semaforo
  • semctl(semid, 0, IPC_RMID);
  • exit(EXIT_SUCCESS);
  • }
  • void controlla_area(int semid, struct sh_data *data)
  • {
  • int area;
  • sem_down(semid);
  • area = data->base * data->altezza;
  • printf("L'area del triangolo è %d\n", area);
  • sem_up(semid);
  • }
  • int sem_set(int semid, int val)
  • {
  • union semun s;
  • s.val = val;
  • /* Inizializza il valore del semaforo */
  • return semctl(semid, 0, SETVAL, s);
  • }
  • int sem_down(int semid)
  • {
  • struct sembuf buff;
  • buff.sem_num = 0;
  • buff.sem_op = -1;
  • buff.sem_flg = SEM_UNDO;
  • /* Decrementa il valore del semaforo */
  • return semop(semid, &buff, 1);
  • }
  • int sem_up(int semid)
  • {
  • struct sembuf buff;
  • buff.sem_num = 0;
  • buff.sem_op = 1;
  • buff.sem_flg = SEM_UNDO;
  • /* Incrementa il valore del semaforo */
  • return semop(semid, &buff, 1);
  • }
  • ```
#3: Post edited by user avatar Alexei‭ · 2022-05-13T15:39:40Z (almost 2 years ago)
added relevant tags + minor fixes
  • This program creates a pid and shares two integers (`base` and `height`) through the shared memory.
  • The parent process time asks four times to insert two integers and wait for the pid to calculate the area.
  • After the four cicles, the ppid waits for the end of its child, deletes the shared memory and the semaphore and ends. The child waits for the new variable (`base`, `height` with semaphore), calculates the area, and prints it, and after the four interations, it ends.
  • I have to use semaphore for process synchronization and to regulate the critical session. The problem consists in the pid, it works only one time.
  • ```c
  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <time.h>
  • #include <unistd.h>
  • #include <sys/wait.h>
  • #include <sys/types.h>
  • #include <sys/ipc.h>
  • #include <sys/sem.h>
  • #include <sys/shm.h>
  • #include <string.h>
  • #define SEM_KEY (key_t)8765
  • #define SHM_KEY (key_t)9876
  • /** Questa union la trovate digitando "man semctl" */
  • union semun
  • {
  • int val; /* Value for SETVAL */
  • struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
  • unsigned short *array; /* Array for GETALL, SETALL */
  • struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */
  • };
  • /** Tipo di dato condiviso */
  • struct sh_data
  • {
  • int altezza;
  • int base;
  • };
  • /** Funzioni di appoggio per operare con i semafori */
  • int sem_set(int semid, int val);
  • int sem_down(int semid);
  • int sem_up(int semid);
  • void controlla_area(int, struct sh_data *);
  • int main()
  • {
  • pid_t pid;
  • int semid, shmid, i;
  • struct sh_data *data;
  • // SEMAFORO
  • // Creazione del semaforo
  • semid = semget(SEM_KEY, 1, 0666 | IPC_CREAT);
  • // Inizializzazione semaforo
  • sem_set(semid, 1);
  • // SHARED MEMORY
  • // Creazione shared memory
  • shmid = shmget(SHM_KEY, sizeof(struct sh_data), 0666 | IPC_CREAT);
  • // Attach della shm allo spazio di indirizzi del processo
  • data = (struct sh_data *)shmat(shmid, NULL, 0);
  • // Processo figlio
  • pid = fork();
  • switch (pid)
  • {
  • case -1:
  • perror("Errore fork");
  • exit(EXIT_FAILURE);
  • case 0:
  • controlla_area(semid, data);
  • exit(EXIT_SUCCESS);
  • default:
  • break;
  • }
  • // Processo padre
  • for (i = 0; i < 4; i++)
  • {
  • sem_down(semid);
  • printf("Inserisci la base: ");
  • scanf("%d", &(data->base));
  • printf("Inserisci l'altezza: ");
  • scanf("%d", &(data->altezza));
  • sem_up(semid);
  • wait(NULL);
  • }
  • // Detach della shared memory
  • shmdt(data);
  • // Eliminazione della shared memory
  • shmctl(shmid, IPC_RMID, NULL);
  • // Eliminazione semaforo
  • semctl(semid, 0, IPC_RMID);
  • exit(EXIT_SUCCESS);
  • }
  • void controlla_area(int semid, struct sh_data *data)
  • {
  • int area;
  • sem_down(semid);
  • area = data->base * data->altezza;
  • printf("L'area del triangolo è %d\n", area);
  • sem_up(semid);
  • }
  • int sem_set(int semid, int val)
  • {
  • union semun s;
  • s.val = val;
  • /* Inizializza il valore del semaforo */
  • return semctl(semid, 0, SETVAL, s);
  • }
  • int sem_down(int semid)
  • {
  • struct sembuf buff;
  • buff.sem_num = 0;
  • buff.sem_op = -1;
  • buff.sem_flg = SEM_UNDO;
  • /* Decrementa il valore del semaforo */
  • return semop(semid, &buff, 1);
  • }
  • int sem_up(int semid)
  • {
  • struct sembuf buff;
  • buff.sem_num = 0;
  • buff.sem_op = 1;
  • buff.sem_flg = SEM_UNDO;
  • /* Incrementa il valore del semaforo */
  • return semop(semid, &buff, 1);
  • }
  • ```
  • This program creates a pid and shares two integers (`base` and `height`) through the shared memory.
  • The parent process time asks four times to insert two integers and wait for the pid to calculate the area.
  • After the four cycles, the ppid waits for the end of its child, deletes the shared memory and the semaphore and ends. The child waits for the new variable (`base`, `height` with semaphore), calculates the area, and prints it, and after the four iterations, it ends.
  • I have to use semaphore for process synchronization and to regulate the critical section. The problem consists of the pid, it works only one time.
  • ```c
  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <time.h>
  • #include <unistd.h>
  • #include <sys/wait.h>
  • #include <sys/types.h>
  • #include <sys/ipc.h>
  • #include <sys/sem.h>
  • #include <sys/shm.h>
  • #include <string.h>
  • #define SEM_KEY (key_t)8765
  • #define SHM_KEY (key_t)9876
  • /** Questa union la trovate digitando "man semctl" */
  • union semun
  • {
  • int val; /* Value for SETVAL */
  • struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
  • unsigned short *array; /* Array for GETALL, SETALL */
  • struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */
  • };
  • /** Tipo di dato condiviso */
  • struct sh_data
  • {
  • int altezza;
  • int base;
  • };
  • /** Funzioni di appoggio per operare con i semafori */
  • int sem_set(int semid, int val);
  • int sem_down(int semid);
  • int sem_up(int semid);
  • void controlla_area(int, struct sh_data *);
  • int main()
  • {
  • pid_t pid;
  • int semid, shmid, i;
  • struct sh_data *data;
  • // SEMAFORO
  • // Creazione del semaforo
  • semid = semget(SEM_KEY, 1, 0666 | IPC_CREAT);
  • // Inizializzazione semaforo
  • sem_set(semid, 1);
  • // SHARED MEMORY
  • // Creazione shared memory
  • shmid = shmget(SHM_KEY, sizeof(struct sh_data), 0666 | IPC_CREAT);
  • // Attach della shm allo spazio di indirizzi del processo
  • data = (struct sh_data *)shmat(shmid, NULL, 0);
  • // Processo figlio
  • pid = fork();
  • switch (pid)
  • {
  • case -1:
  • perror("Errore fork");
  • exit(EXIT_FAILURE);
  • case 0:
  • controlla_area(semid, data);
  • exit(EXIT_SUCCESS);
  • default:
  • break;
  • }
  • // Processo padre
  • for (i = 0; i < 4; i++)
  • {
  • sem_down(semid);
  • printf("Inserisci la base: ");
  • scanf("%d", &(data->base));
  • printf("Inserisci l'altezza: ");
  • scanf("%d", &(data->altezza));
  • sem_up(semid);
  • wait(NULL);
  • }
  • // Detach della shared memory
  • shmdt(data);
  • // Eliminazione della shared memory
  • shmctl(shmid, IPC_RMID, NULL);
  • // Eliminazione semaforo
  • semctl(semid, 0, IPC_RMID);
  • exit(EXIT_SUCCESS);
  • }
  • void controlla_area(int semid, struct sh_data *data)
  • {
  • int area;
  • sem_down(semid);
  • area = data->base * data->altezza;
  • printf("L'area del triangolo è %d\n", area);
  • sem_up(semid);
  • }
  • int sem_set(int semid, int val)
  • {
  • union semun s;
  • s.val = val;
  • /* Inizializza il valore del semaforo */
  • return semctl(semid, 0, SETVAL, s);
  • }
  • int sem_down(int semid)
  • {
  • struct sembuf buff;
  • buff.sem_num = 0;
  • buff.sem_op = -1;
  • buff.sem_flg = SEM_UNDO;
  • /* Decrementa il valore del semaforo */
  • return semop(semid, &buff, 1);
  • }
  • int sem_up(int semid)
  • {
  • struct sembuf buff;
  • buff.sem_num = 0;
  • buff.sem_op = 1;
  • buff.sem_flg = SEM_UNDO;
  • /* Incrementa il valore del semaforo */
  • return semop(semid, &buff, 1);
  • }
  • ```
#2: Post edited by user avatar hkotsubo‭ · 2022-05-13T15:15:29Z (almost 2 years ago)
rephrasing/fixes grammar issues, fix code block
  • The pid does work only one time after the two scanf.
  • The pid works only once after the two calls to scanf
  • So in this program the ppid crate a pid and with him share two interger("base and height") trought the shared memory.The parent processo for 4 time askt to the users to insert two integer and wait for the pid to calculate the area, after the 4 cicles the ppid wait for the end of its son, delete the shared memory and the semaphore and end its. The son for 4 time wait for the new variable(base,height with semaphore), calculates the area, and printf the area, after the 4 interation end itself. I have to use semaphore for the sincronization of the processo e to regulate the critical session. The problem consist in the pid that work only one time.
  • ````````
  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <time.h>
  • #include <unistd.h>
  • #include <sys/wait.h>
  • #include <sys/types.h>
  • #include <sys/ipc.h>
  • #include <sys/sem.h>
  • #include <sys/shm.h>
  • #include <string.h>
  • #define SEM_KEY (key_t)8765
  • #define SHM_KEY (key_t)9876
  • /** Questa union la trovate digitando "man semctl" */
  • union semun
  • {
  • int val; /* Value for SETVAL */
  • struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
  • unsigned short *array; /* Array for GETALL, SETALL */
  • struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */
  • };
  • /** Tipo di dato condiviso */
  • struct sh_data
  • {
  • int altezza;
  • int base;
  • };
  • /** Funzioni di appoggio per operare con i semafori */
  • int sem_set(int semid, int val);
  • int sem_down(int semid);
  • int sem_up(int semid);
  • void controlla_area(int, struct sh_data *);
  • int main()
  • {
  • pid_t pid;
  • int semid, shmid, i;
  • struct sh_data *data;
  • // SEMAFORO
  • // Creazione del semaforo
  • semid = semget(SEM_KEY, 1, 0666 | IPC_CREAT);
  • // Inizializzazione semaforo
  • sem_set(semid, 1);
  • // SHARED MEMORY
  • // Creazione shared memory
  • shmid = shmget(SHM_KEY, sizeof(struct sh_data), 0666 | IPC_CREAT);
  • // Attach della shm allo spazio di indirizzi del processo
  • data = (struct sh_data *)shmat(shmid, NULL, 0);
  • // Processo figlio
  • pid = fork();
  • switch (pid)
  • {
  • case -1:
  • perror("Errore fork");
  • exit(EXIT_FAILURE);
  • case 0:
  • controlla_area(semid, data);
  • exit(EXIT_SUCCESS);
  • default:
  • break;
  • }
  • // Processo padre
  • for (i = 0; i < 4; i++)
  • {
  • sem_down(semid);
  • printf("Inserisci la base: ");
  • scanf("%d", &(data->base));
  • printf("Inserisci l'altezza: ");
  • scanf("%d", &(data->altezza));
  • sem_up(semid);
  • wait(NULL);
  • }
  • // Detach della shared memory
  • shmdt(data);
  • // Eliminazione della shared memory
  • shmctl(shmid, IPC_RMID, NULL);
  • // Eliminazione semaforo
  • semctl(semid, 0, IPC_RMID);
  • exit(EXIT_SUCCESS);
  • }
  • void controlla_area(int semid, struct sh_data *data)
  • {
  • int area;
  • sem_down(semid);
  • area = data->base * data->altezza;
  • printf("L'area del triangolo è %d\n", area);
  • sem_up(semid);
  • }
  • int sem_set(int semid, int val)
  • {
  • union semun s;
  • s.val = val;
  • /* Inizializza il valore del semaforo */
  • return semctl(semid, 0, SETVAL, s);
  • }
  • int sem_down(int semid)
  • {
  • struct sembuf buff;
  • buff.sem_num = 0;
  • buff.sem_op = -1;
  • buff.sem_flg = SEM_UNDO;
  • `````
  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <time.h>
  • #include <unistd.h>
  • #include <sys/wait.h>
  • #include <sys/types.h>
  • #include <sys/ipc.h>
  • #include <sys/sem.h>
  • #include <sys/shm.h>
  • #include <string.h>
  • #define SEM_KEY (key_t)8765
  • #define SHM_KEY (key_t)9876
  • /** Questa union la trovate digitando "man semctl" */
  • union semun
  • {
  • int val; /* Value for SETVAL */
  • struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
  • unsigned short *array; /* Array for GETALL, SETALL */
  • struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */
  • };
  • /** Tipo di dato condiviso */
  • struct sh_data
  • {
  • int altezza;
  • int base;
  • };
  • /** Funzioni di appoggio per operare con i semafori */
  • int sem_set(int semid, int val);
  • int sem_down(int semid);
  • int sem_up(int semid);
  • void controlla_area(int, struct sh_data *);
  • int main()
  • {
  • pid_t pid;
  • int semid, shmid, i;
  • struct sh_data *data;
  • // SEMAFORO
  • // Creazione del semaforo
  • semid = semget(SEM_KEY, 1, 0666 | IPC_CREAT);
  • // Inizializzazione semaforo
  • sem_set(semid, 1);
  • // SHARED MEMORY
  • // Creazione shared memory
  • shmid = shmget(SHM_KEY, sizeof(struct sh_data), 0666 | IPC_CREAT);
  • // Attach della shm allo spazio di indirizzi del processo
  • data = (struct sh_data *)shmat(shmid, NULL, 0);
  • // Processo figlio
  • pid = fork();
  • switch (pid)
  • {
  • case -1:
  • perror("Errore fork");
  • exit(EXIT_FAILURE);
  • case 0:
  • controlla_area(semid, data);
  • exit(EXIT_SUCCESS);
  • default:
  • break;
  • }
  • // Processo padre
  • for (i = 0; i < 4; i++)
  • {
  • sem_down(semid);
  • printf("Inserisci la base: ");
  • scanf("%d", &(data->base));
  • printf("Inserisci l'altezza: ");
  • scanf("%d", &(data->altezza));
  • sem_up(semid);
  • wait(NULL);
  • }
  • // Detach della shared memory
  • shmdt(data);
  • // Eliminazione della shared memory
  • shmctl(shmid, IPC_RMID, NULL);
  • // Eliminazione semaforo
  • semctl(semid, 0, IPC_RMID);
  • exit(EXIT_SUCCESS);
  • }
  • void controlla_area(int semid, struct sh_data *data)
  • {
  • int area;
  • sem_down(semid);
  • area = data->base * data->altezza;
  • printf("L'area del triangolo è %d\n", area);
  • sem_up(semid);
  • }
  • int sem_set(int semid, int val)
  • {
  • union semun s;
  • s.val = val;
  • /* Inizializza il valore del semaforo */
  • return semctl(semid, 0, SETVAL, s);
  • }
  • int sem_down(int semid)
  • {
  • struct sembuf buff;
  • buff.sem_num = 0;
  • buff.sem_op = -1;
  • buff.sem_flg = SEM_UNDO;
  • /* Decrementa il valore del semaforo */
  • return semop(semid, &buff, 1);
  • }
  • int sem_up(int semid)
  • {
  • struct sembuf buff;
  • buff.sem_num = 0;
  • buff.sem_op = 1;
  • buff.sem_flg = SEM_UNDO;
  • /* Incrementa il valore del semaforo */
  • return semop(semid, &buff, 1);
  • }`
  • This program creates a pid and shares two integers (`base` and `height`) through the shared memory.
  • The parent process time asks four times to insert two integers and wait for the pid to calculate the area.
  • After the four cicles, the ppid waits for the end of its child, deletes the shared memory and the semaphore and ends. The child waits for the new variable (`base`, `height` with semaphore), calculates the area, and prints it, and after the four interations, it ends.
  • I have to use semaphore for process synchronization and to regulate the critical session. The problem consists in the pid, it works only one time.
  • ```c
  • #include <stdio.h>
  • #include <stdlib.h>
  • #include <time.h>
  • #include <unistd.h>
  • #include <sys/wait.h>
  • #include <sys/types.h>
  • #include <sys/ipc.h>
  • #include <sys/sem.h>
  • #include <sys/shm.h>
  • #include <string.h>
  • #define SEM_KEY (key_t)8765
  • #define SHM_KEY (key_t)9876
  • /** Questa union la trovate digitando "man semctl" */
  • union semun
  • {
  • int val; /* Value for SETVAL */
  • struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
  • unsigned short *array; /* Array for GETALL, SETALL */
  • struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */
  • };
  • /** Tipo di dato condiviso */
  • struct sh_data
  • {
  • int altezza;
  • int base;
  • };
  • /** Funzioni di appoggio per operare con i semafori */
  • int sem_set(int semid, int val);
  • int sem_down(int semid);
  • int sem_up(int semid);
  • void controlla_area(int, struct sh_data *);
  • int main()
  • {
  • pid_t pid;
  • int semid, shmid, i;
  • struct sh_data *data;
  • // SEMAFORO
  • // Creazione del semaforo
  • semid = semget(SEM_KEY, 1, 0666 | IPC_CREAT);
  • // Inizializzazione semaforo
  • sem_set(semid, 1);
  • // SHARED MEMORY
  • // Creazione shared memory
  • shmid = shmget(SHM_KEY, sizeof(struct sh_data), 0666 | IPC_CREAT);
  • // Attach della shm allo spazio di indirizzi del processo
  • data = (struct sh_data *)shmat(shmid, NULL, 0);
  • // Processo figlio
  • pid = fork();
  • switch (pid)
  • {
  • case -1:
  • perror("Errore fork");
  • exit(EXIT_FAILURE);
  • case 0:
  • controlla_area(semid, data);
  • exit(EXIT_SUCCESS);
  • default:
  • break;
  • }
  • // Processo padre
  • for (i = 0; i < 4; i++)
  • {
  • sem_down(semid);
  • printf("Inserisci la base: ");
  • scanf("%d", &(data->base));
  • printf("Inserisci l'altezza: ");
  • scanf("%d", &(data->altezza));
  • sem_up(semid);
  • wait(NULL);
  • }
  • // Detach della shared memory
  • shmdt(data);
  • // Eliminazione della shared memory
  • shmctl(shmid, IPC_RMID, NULL);
  • // Eliminazione semaforo
  • semctl(semid, 0, IPC_RMID);
  • exit(EXIT_SUCCESS);
  • }
  • void controlla_area(int semid, struct sh_data *data)
  • {
  • int area;
  • sem_down(semid);
  • area = data->base * data->altezza;
  • printf("L'area del triangolo è %d\n", area);
  • sem_up(semid);
  • }
  • int sem_set(int semid, int val)
  • {
  • union semun s;
  • s.val = val;
  • /* Inizializza il valore del semaforo */
  • return semctl(semid, 0, SETVAL, s);
  • }
  • int sem_down(int semid)
  • {
  • struct sembuf buff;
  • buff.sem_num = 0;
  • buff.sem_op = -1;
  • buff.sem_flg = SEM_UNDO;
  • /* Decrementa il valore del semaforo */
  • return semop(semid, &buff, 1);
  • }
  • int sem_up(int semid)
  • {
  • struct sembuf buff;
  • buff.sem_num = 0;
  • buff.sem_op = 1;
  • buff.sem_flg = SEM_UNDO;
  • /* Incrementa il valore del semaforo */
  • return semop(semid, &buff, 1);
  • }
  • ```
#1: Initial revision by user avatar Luca_Impellizzeri‭ · 2022-05-13T13:38:45Z (almost 2 years ago)
The pid does work only one time after the two scanf.
So in this program the ppid crate a pid and with him share two interger("base and height") trought the shared memory.The parent processo for 4 time askt to the users to insert two integer and wait for the pid to calculate the area, after the 4 cicles the ppid wait for the end of its son, delete the shared memory and the semaphore and end its. The son for 4 time wait for the new variable(base,height with semaphore), calculates the area, and printf the area, after the 4 interation end itself. I have to use semaphore for the sincronization of the processo e to regulate the critical session. The problem consist in the pid that work only one time.
````````
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <string.h>

#define SEM_KEY (key_t)8765
#define SHM_KEY (key_t)9876

/** Questa union la trovate digitando "man semctl" */
union semun
{
    int val;               /* Value for SETVAL */
    struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
    unsigned short *array; /* Array for GETALL, SETALL */
    struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */
};

/** Tipo di dato condiviso */
struct sh_data
{
    int altezza;
    int base;
};

/** Funzioni di appoggio per operare con i semafori */
int sem_set(int semid, int val);
int sem_down(int semid);
int sem_up(int semid);

void controlla_area(int, struct sh_data *);

int main()
{
    pid_t pid;
    int semid, shmid, i;
    struct sh_data *data;

    // SEMAFORO
    // Creazione del semaforo
    semid = semget(SEM_KEY, 1, 0666 | IPC_CREAT);

    // Inizializzazione semaforo
    sem_set(semid, 1);

    // SHARED MEMORY
    // Creazione shared memory
    shmid = shmget(SHM_KEY, sizeof(struct sh_data), 0666 | IPC_CREAT);

    // Attach della shm allo spazio di indirizzi del processo
    data = (struct sh_data *)shmat(shmid, NULL, 0);

    // Processo figlio
    pid = fork();
    switch (pid)
    {
    case -1:
        perror("Errore fork");
        exit(EXIT_FAILURE);
    case 0:
        controlla_area(semid, data);
        exit(EXIT_SUCCESS);
    default:
        break;
    }

    // Processo padre
    for (i = 0; i < 4; i++)
    {
        sem_down(semid);
        printf("Inserisci la base: ");
        scanf("%d", &(data->base));
        printf("Inserisci l'altezza: ");
        scanf("%d", &(data->altezza));
        sem_up(semid);
        wait(NULL);
    }

    // Detach della shared memory
    shmdt(data);

    // Eliminazione della shared memory
    shmctl(shmid, IPC_RMID, NULL);

    // Eliminazione semaforo
    semctl(semid, 0, IPC_RMID);

    exit(EXIT_SUCCESS);
}

void controlla_area(int semid, struct sh_data *data)
{
    int area;

    sem_down(semid);
    area = data->base * data->altezza;
    printf("L'area del triangolo è %d\n", area);
    sem_up(semid);
}

int sem_set(int semid, int val)
{
    union semun s;
    s.val = val;
    /* Inizializza il valore del semaforo */
    return semctl(semid, 0, SETVAL, s);
}

int sem_down(int semid)
{
    struct sembuf buff;
    buff.sem_num = 0;
    buff.sem_op = -1;
    buff.sem_flg = SEM_UNDO;
`````
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <string.h>

#define SEM_KEY (key_t)8765
#define SHM_KEY (key_t)9876

/** Questa union la trovate digitando "man semctl" */
union semun
{
    int val;               /* Value for SETVAL */
    struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
    unsigned short *array; /* Array for GETALL, SETALL */
    struct seminfo *__buf; /* Buffer for IPC_INFO(Linux-specific) */
};

/** Tipo di dato condiviso */
struct sh_data
{
    int altezza;
    int base;
};

/** Funzioni di appoggio per operare con i semafori */
int sem_set(int semid, int val);
int sem_down(int semid);
int sem_up(int semid);

void controlla_area(int, struct sh_data *);

int main()
{
    pid_t pid;
    int semid, shmid, i;
    struct sh_data *data;

    // SEMAFORO
    // Creazione del semaforo
    semid = semget(SEM_KEY, 1, 0666 | IPC_CREAT);

    // Inizializzazione semaforo
    sem_set(semid, 1);

    // SHARED MEMORY
    // Creazione shared memory
    shmid = shmget(SHM_KEY, sizeof(struct sh_data), 0666 | IPC_CREAT);

    // Attach della shm allo spazio di indirizzi del processo
    data = (struct sh_data *)shmat(shmid, NULL, 0);

    // Processo figlio
    pid = fork();
    switch (pid)
    {
    case -1:
        perror("Errore fork");
        exit(EXIT_FAILURE);
    case 0:
        controlla_area(semid, data);
        exit(EXIT_SUCCESS);
    default:
        break;
    }

    // Processo padre
    for (i = 0; i < 4; i++)
    {
        sem_down(semid);
        printf("Inserisci la base: ");
        scanf("%d", &(data->base));
        printf("Inserisci l'altezza: ");
        scanf("%d", &(data->altezza));
        sem_up(semid);
        wait(NULL);
    }

    // Detach della shared memory
    shmdt(data);

    // Eliminazione della shared memory
    shmctl(shmid, IPC_RMID, NULL);

    // Eliminazione semaforo
    semctl(semid, 0, IPC_RMID);

    exit(EXIT_SUCCESS);
}

void controlla_area(int semid, struct sh_data *data)
{
    int area;

    sem_down(semid);
    area = data->base * data->altezza;
    printf("L'area del triangolo è %d\n", area);
    sem_up(semid);
}

int sem_set(int semid, int val)
{
    union semun s;
    s.val = val;
    /* Inizializza il valore del semaforo */
    return semctl(semid, 0, SETVAL, s);
}

int sem_down(int semid)
{
    struct sembuf buff;
    buff.sem_num = 0;
    buff.sem_op = -1;
    buff.sem_flg = SEM_UNDO;
    /* Decrementa il valore del semaforo */
    return semop(semid, &buff, 1);
}

int sem_up(int semid)
{
    struct sembuf buff;
    buff.sem_num = 0;
    buff.sem_op = 1;
    buff.sem_flg = SEM_UNDO;
    /* Incrementa il valore del semaforo */
    return semop(semid, &buff, 1);
}`
c