Unit – III Process
Programs
1. Write a C program to create a child process and allow the
parent to display “parent” and the child to display “child” on the screen.
#include
<stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
int
main(int argc, char *argv[])
{
int
pid;
int
p[2];
int
q[2];
int
a;
int
b;
a
= pipe(p);
if(a
== -1)
{
fprintf(stderr,
"Pipe Failed.\n");
return
EXIT_FAILURE;
}
b
= pipe(q);
if(b
== -1)
{
fprintf(stderr,
"Pipe Failed.\n");
return
EXIT_FAILURE;
}
pid
= fork();
switch(pid){
case
-1: perror("main: fork");
exit(1);
case
0: printf("Child process ID: %d\n", pid); break;
default:
printf("Parent process ID: %d\n", pid); break;
}
getchar();
return
0;
}
2. Write a C program to create a Zombie process.
#include
<stdlib.h>
#include
<sys/types.h>
#include
<unistd.h>
int
main ()
{
pid_t
child_pid;
child_pid
= fork ();
if
(child_pid > 0) {
sleep
(60);
}
else {
exit
(0);
}
return
0;
}
3. Write a C program that illustrates how an orphan is
created.
#include
<stdio.h>
#include
<unistd.h>
#include
<sys/types.h>
#include
<stdlib.h>
#include
<sys/wait.h>
int
main()
{
pid_t
i;
i=fork();
if
(i==0)
{
while
(1)
{
printf("child
process pid = %d, ppid=%d\n",(int)getpid(), (int)getppid());
sleep(3);
}
}
else
{
printf("parent
has finished");
}
return 0;
Unit –IV IPC
PIPE and FIFO
1. Write a C program that illustrates how to execute two
commands concurrently with a command pipe. Eg. ls-l|sort.
#include<stdio.h>
#include<stdlib.h>
int
main(int argc,char *argv[])
{
int
fd[2],pid,k;
k=pipe(fd);
if(k==-1)
{
perror("pipe");
exit(1);
}
pid=fork();
if(pid==0)
{
close(fd[0]);
dup2(fd[1],1);
close(fd[1]);
execlp(argv[1],argv[1],NULL);
perror("execl");
}
else
{
wait(2);
close(fd[1]);
dup2(fd[0],0);
close(fd[0]);
execlp(argv[2],argv[2],NULL);
perror("execl");
}
}
2. Write a C program that illustrate communication between
two unrelated processes using named pipe (FIFO file).
reader.c
#include
<fcntl.h>
#include
<stdio.h>
#include
<sys/stat.h>
#include
<unistd.h>
#define
MAX_BUF 1024
int
main()
{
int
fd;
char
* myfifo = "/tmp/myfifo";
char
buf[MAX_BUF];
fd
= open(myfifo, O_RDONLY);
read(fd,
buf, MAX_BUF);
printf("Received:
%s\n", buf);
close(fd);
return
0;
}
writer.c
#include
<fcntl.h>
#include
<sys/stat.h>
#include
<sys/types.h>
#include
<unistd.h>
int
main()
{
int
fd;
char
* myfifo = "/tmp/myfifo";
mkfifo(myfifo,
0666);
fd
= open(myfifo, O_WRONLY);
write(fd,
"Hi", sizeof("Hi"));
close(fd);
unlink(myfifo);
return
0;
}
#include
<stdio.h>
#define
READ 0
/*
The index of the “read” end of the pipe */
#define
WRITE 1
/*
The index of the “write” end of the pipe */
char
* phrase = "Hello welcome to CSE, SRITW, Warangal";
main
()
{
int
fd[2], bytesRead;
char
message [100]; /* Parent process’s message buffer */
pipe
( fd ); /*Create an unnamed pipe*/
if
( fork ( ) == 0 ) /* Child Writer */
{
close
(fd[READ]); /* Close unused end*/
write
(fd[WRITE], phrase,strlen ( phrase) +1); /* include NULL*/
close
(fd[WRITE]); /* Close used end*/
}
else
/* Parent Reader */
{
close
(fd[WRITE]); /* Close unused end*/ bytesRead = read ( fd[READ], message, 100);
printf
( "Read %d bytes: %s\n", bytesRead, message);
close
( fd[READ]); /* Close used end */
}
}
Message Queue
send.c
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
int
qid,len,i;
char
s[15];
struct
{
long
mtype;
char
mtext[15];
}
message,buff;
qid=msgget((key_t)10,IPC_CREAT|0666);
if(qid==-1)
{
perror("message
queue create failed");
exit(1);
}
for(i=1;i<=3;i++)
{
printf("Enter
the message to send\n");
scanf("%s",s);
strcpy(message.mtext,s);
message.mtype=i;
len=strlen(message.mtext);
if(msgsnd(qid,&message,len+1,0)==-1)
{
perror("message
failed\n");
exit(1);
} } }
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
int
qid,len,i;
char
s[15];
struct
{
long
mtype;
char
mtext[15];
}buff;
qid=msgget((key_t)10,IPC_CREAT|0666);
if(qid==-1)
{
perror("message
queue create failed");
exit(0);
}
for(i=1;i<=3;i++)
{
if(msgrcv(qid,&buff,15,i,0)==-1)
{
perror("message
failed\n");
exit(0);
}
printf("Message
received from sender is %s\n",buff.mtext);
}
}}
Shared Memory
#include<stdio.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/shm.h>
#define
SIZE 5*1024
main()
{
char *ptr;
int
shmid,pid,n;
shmid=shmget((key_t)10,30,IPC_CREAT|0666);
ptr=(char
*)shmat(shmid,(char *)0,0);
pid=fork();
if(pid==0)
{
n=read(0,ptr,SIZE);
ptr[n]='\0';
} else {
wait(0) ;
printf("parent
read from shared memory\n");
write(1,ptr,strlen(ptr));
}
}
Semaphore
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<unistd.h>
#define num_loops 2
int main(int argc,char*
argv[])
{
int sem_set_id;
int child_pid,i,sem_val;
struct sembuf sem_op;
int rc;
struct timespec delay;
clrscr();
sem_set_id=semget(ipc_private,2,0600);
if(sem_set_id==-1)
{
perror(“main:semget”);
exit(1);
}
printf(“semaphore set
created,semaphore setid‘%d’\n ”,
sem_set_id);
child_pid=fork();
switch(child_pid)
{
case -1:
perror(“fork”);
exit(1);
case 0:
for(i=0;i<num_loops;i++)
{
sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
printf(“producer:’%d’\n”,i);
fflush(stdout);
}
break;
default:
for(i=0;i<num_loops;i++){
printf(“consumer:’%d’\n”,i);
fflush(stdout);
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
if(rand()>3*(rano_max14)){
delay.tv_sec=0;
delay.tv_nsec=10;
nanosleep(&delay,null);
}}
break;
}return 0;}
Sockets with UNIX
domain
Client:
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
struct sockaddr_un address;
int socket_fd, nbytes;
char buffer[256];
socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(socket_fd < 0)
{
printf("socket() failed\n");
return 1;
} /* start with a clean address structure */
memset(&address, 0, sizeof(struct sockaddr_un));
address.sun_family = AF_UNIX;
snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
if(connect(socket_fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)) != 0)
{
printf("connect() failed\n");
return 1;
}
nbytes = snprintf(buffer, 256, "hello from a client");
write(socket_fd, buffer, nbytes);
nbytes = read(socket_fd, buffer, 256);
buffer[nbytes] = 0;
printf("MESSAGE FROM SERVER: %s\n", buffer);
close(socket_fd);
return 0;
}
Server:
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
int connection_handler(int connection_fd)
{
int nbytes;
char buffer[256];
nbytes = read(connection_fd, buffer, 256);
buffer[nbytes] = 0;
printf("MESSAGE FROM CLIENT: %s\n", buffer);
nbytes = snprintf(buffer, 256, "hello from the server");
write(connection_fd, buffer, nbytes);
close(connection_fd);
return 0;
}
int main(void)
{
struct sockaddr_un address;
int socket_fd, connection_fd;
socklen_t address_length;
pid_t child;
socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(socket_fd < 0)
{
printf("socket() failed\n");
return 1;
}
unlink("./demo_socket"); /* start with a clean address structure */
memset(&address, 0, sizeof(struct sockaddr_un));
address.sun_family = AF_UNIX;
snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
if(bind(socket_fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)) != 0)
{
printf("bind() failed\n");
return 1;
}
if(listen(socket_fd, 5) != 0)
{
printf("listen() failed\n");
return 1;
}
while((connection_fd = accept(socket_fd, (struct sockaddr *) &address, &address_length)) > -1)
{
child = fork();
if(child == 0)
{ /* now inside newly created connection handling process */
return connection_handler(connection_fd);
}/* still inside server process */
close(connection_fd);
}
close(socket_fd);
unlink("./demo_socket");
return 0;
}
Sockets with INTERNET
domain
Client:
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
struct sockaddr_un address;
int socket_fd, nbytes;
char buffer[256];
socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(socket_fd < 0)
{
printf("socket() failed\n");
return 1;
} /* start with a clean address structure */
memset(&address, 0, sizeof(struct sockaddr_un));
address.sun_family = AF_UNIX;
snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
if(connect(socket_fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)) != 0)
{
printf("connect() failed\n");
return 1;
}
nbytes = snprintf(buffer, 256, "hello from a client");
write(socket_fd, buffer, nbytes);
nbytes = read(socket_fd, buffer, 256);
buffer[nbytes] = 0;
printf("MESSAGE FROM SERVER: %s\n", buffer);
close(socket_fd);
return 0;
}
Server:
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
int connection_handler(int connection_fd)
{
int nbytes;
char buffer[256];
nbytes = read(connection_fd, buffer, 256);
buffer[nbytes] = 0;
printf("MESSAGE FROM CLIENT: %s\n", buffer);
nbytes = snprintf(buffer, 256, "hello from the server");
write(connection_fd, buffer, nbytes);
close(connection_fd);
return 0;
}
int main(void)
{
struct sockaddr_un address;
int socket_fd, connection_fd;
socklen_t address_length;
pid_t child;
socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(socket_fd < 0)
{
printf("socket() failed\n");
return 1;
}
unlink("./demo_socket"); /* start with a clean address structure */
memset(&address, 0, sizeof(struct sockaddr_un));
address.sun_family = AF_UNIX;
snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
if(bind(socket_fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)) != 0)
{
printf("bind() failed\n");
return 1;
}
if(listen(socket_fd, 5) != 0)
{
printf("listen() failed\n");
return 1;
}
while((connection_fd = accept(socket_fd, (struct sockaddr *) &address, &address_length)) > -1)
{
child = fork();
if(child == 0)
{ /* now inside newly created connection handling process */
return connection_handler(connection_fd);
}/* still inside server process */
close(connection_fd);
}
close(socket_fd);
unlink("./demo_socket");
return 0;
}
No comments:
Post a Comment