0% found this document useful (0 votes)
2 views

Assignment 7 & 8

The document outlines experiments for an Operating System lab, focusing on zombie processes and the exec() system call, as well as shared memory concepts. It includes C programs to demonstrate creating a zombie process, using exec() to replace a process, and utilizing shared memory for inter-process communication. Each section provides code snippets, expected outputs, and explanations of the relevant system calls.

Uploaded by

Barun Banerjee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 7 & 8

The document outlines experiments for an Operating System lab, focusing on zombie processes and the exec() system call, as well as shared memory concepts. It includes C programs to demonstrate creating a zombie process, using exec() to replace a process, and utilizing shared memory for inter-process communication. Each section provides code snippets, expected outputs, and explanations of the relevant system calls.

Uploaded by

Barun Banerjee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ASANSOL ENGINEERING COLLEGE

Department of Computer Science and Engineering


Course Name: Operating Experiment No. 7
System Lab

Course Code : PCC-CS592 Branch: CSE Semester: 5

AIM: Knowledge on zombie and exec () system call

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.

Problem:Write a c program to create an 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);

}
}

Operating System Lab Manual PCC-CS 592 Page 1


ASANSOL ENGINEERING COLLEGE
Department of Computer Science and Engineering
Output:
I am Child
I am Child
I am Child
I am Child
I am Child
I am Parent

USE COMMAND :-ps -eaf

Replacing the process by exec system call


The exec() family of functions replaces the current process image with a new process image. It loads the
program into the current process space and runs it from the entry point.
The exec() family consists of following functions:-
execl,execlp, execle, execv, execvp, execvpe

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

Course Code : PCC-CS592 Branch: CSE Semester: 5

AIM: Knowledge on Shared Memory.

Shared Memory:
Shared memory is a memory shared between two or more processes.

System call related to shared memory

ftok(): is use to generate a unique key.


shmget(): int shmget(key_t,size_tsize,intshmflg);
Create a shared memory segment
Retrns ID of segment : shmid
key:-Unique identifier of the shared memory segment.
size:- Size of the shared memory
void shmat(int shmid ,void *shmaddr ,int shmflg);
shmid is shared memory id use to attach the shared memory segment with calling
process.
shmaddr specifies specific address to use but we should set
it to zero and OS will automatically choose the address.
int shmdt(void *shmaddr):
detach itself from memory segment
shmctl(): when you detach from shared memory,it is not destroyed. So, to destroy
shmctl() is used. shmctl(int shmid,IPC_RMID,NULL);

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:

Writer process attached to shared memory segment


Writer wrote: Hello from writer!

Reader terminal:

Reader process attached to shared memory segment


Reader read: Hello from writer!

Operating System Lab Manual PCC-CS 592 Page 5

You might also like