Assignment 7 & 8
Assignment 7 & 8
Zombie Process:
A zombie process is a process whose execution is completed but it still has an entry in the process table.
Zombie processes usually occur for child processes, as the parent process still needs to read its child‘s exit
status. Once this is done using the wait system call, the zombie process is eliminated from the process
table. This is known as reaping the zombie process.
Program:
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
int main()
{
int i;
int pid = fork();
if (pid==0)
{
for (i=0; i<5; i++)
printf("I am Child\n");
}
else
{
wait(NULL);
printf("I am Parent\n");
while(1);
}
}
Problem:Write a c program to display the pid of replaced process using exec() system call.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {
fprintf(stderr, "fork() failed\n");
exit(1);
} else if (pid == 0) {
printf("Child PID: %d\n", getpid());
execl("/bin/ls", "ls", NULL);
fprintf(stderr, "execl() failed\n");
exit(1);
} else {
printf("Parent PID: %d\n", getpid());
printf("Child (replaced) PID: %d\n", pid);
wait(NULL);
}
return 0;
}
Output:
You'll see the PIDs of the parent and child processes, followed by the output of the ls command
(which replaced the child process).
Operating System Lab Manual PCC-CS 592 Page 2
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
Course Name: Operating Experiment No. 8
System Lab
Shared Memory:
Shared memory is a memory shared between two or more processes.
Problem:
Write two c program writer and reader which use the shared memory, to write and read massage .
Program:
writer.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/types.h>
#define SHM_KEY 0x1234
#define SHM_SIZE 1024
int
main() {
int shm_id;
char *shm_addr;
Operating System Lab Manual PCC-CS 592 Page 3
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
shm_id = shmget(SHM_KEY, SHM_SIZE, IPC_CREAT | 0666);
if (shm_id < 0)
{
perror("shmget");
exit(1);
}
shm_addr = shmat(shm_id, NULL, 0);
if (shm_addr == (char *)-1)
{
perror("shmat");
exit(1);
}
printf("Writer process attached to shared memory segment\n");
char message[] = "Hello from writer!";
strcpy(shm_addr, message);
printf("Writer wrote: %s\n", shm_addr);
if (shmdt(shm_addr) < 0) {
perror("shmdt");
exit(1);
}
return 0;
}
reader.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/types.h>
#define SHM_KEY 0x1234
#define SHM_SIZE 1024
int main() {
int shm_id;
char *shm_addr;
shm_id = shmget(SHM_KEY, SHM_SIZE, 0);
if (shm_id < 0) {
perror("shmget");
exit(1);
}
shm_addr = shmat(shm_id, NULL, 0);
if (shm_addr == (char *)-1) {
perror("shmat");
exit(1);
}
printf("Reader process attached to shared memory segment\n");
printf("Reader read: %s\n", shm_addr);
if (shmdt(shm_addr) < 0) {
perror("shmdt");
exit(1);
}
Operating System Lab Manual PCC-CS 592 Page 4
ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
return 0;
}
Output:
Writer terminal:
Reader terminal: