You are on page 1of 30

WEEK-1

IMPLEMENT THE FOLLOWING FORMS OF IPC.

a)pipes

b)FIFO

a)PIPES:

PROGRAM:

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <sys/types.h>

#include <unistd.h>

int main(void)

int pfds[2];

char buf[30];

pipe(pfds);

if (!fork()) {

printf(" CHILD: writing to the pipe\n");

write(pfds[1], "test", 5);

printf(" CHILD: exiting\n");

exit(0);

} else {

printf("PARENT: reading from pipe\n");

1
read(pfds[0], buf, 5);

printf("PARENT: read \"%s\"\n", buf);

wait(NULL);

return 0;

2
OUTPUT:

3
b)FIFO:

PROGRAM:

client:
#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#define FIFO_NAME "me_maid"

int main(void)

char s[300];

int num, fd;

mknod(FIFO_NAME, S_IFIFO | 0666, 0);

printf("waiting for readers...\n");

fd = open(FIFO_NAME, O_WRONLY);

printf("got a reader--type some stuff\n");

while (gets(s), !feof(stdin)) {

if ((num = write(fd, s, strlen(s))) == -1)

perror("write");

else

printf("speak: wrote %d bytes\n", num);

4
}

return 0;

Server :

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#define FIFO_NAME "me_maid"

int main(void)

char s[300];

int num, fd;

mknod(FIFO_NAME, S_IFIFO | 0666, 0);

printf("waiting for writers...\n");

fd = open(FIFO_NAME, O_RDONLY);

printf("got a writer\n");

do {

if ((num = read(fd, s, 300)) == -1)

perror("read");

else {

s[num] = '\0';

5
printf("tick: read %d bytes: \"%s\"\n", num, s);

} while (num > 0);

return 0;

6
OUTPUT:

7
WEEK-2

IMPLEMENT FILE TRANSFER USING MESSAGE QUEUE FROM IPC

PROGRAM:

Client
#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

#include <stdio.h>

#include <string.h>

#define MSGSZ 128

typedef struct msgbuf {

long mtype;

char mtext[MSGSZ];

} message_buf;

main()

int msqid;

int msgflg = IPC_CREAT | 0666;

key_t key;

message_buf sbuf;

size_t buf_length;

key = 1234;

(void) fprintf(stderr, "\nmsgget: Calling msgget(%#lx,\%#o)\n",key, msgflg);

if ((msqid = msgget(key, msgflg )) < 0) {

perror("msgget");

8
exit(1);

else

(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);

sbuf.mtype = 1;

(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);

(void) strcpy(sbuf.mtext, "Did you get this?");

(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);

buf_length = strlen(sbuf.mtext) + 1 ;

if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {

printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buf_length);

perror("msgsnd");

exit(1);

else

printf("Message: \"%s\" Sent\n", sbuf.mtext);

exit(0);

9
Server:

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

#include <stdio.h>

#define MSGSZ 128

typedef struct msgbuf {

long mtype;

char mtext[MSGSZ];

} message_buf;

main()

int msqid;

key_t key;

message_buf rbuf;

key = 1234;

if ((msqid = msgget(key, 0666)) < 0)

{ perror("msgget");

exit(1); }

if (msgrcv(msqid, &rbuf, MSGSZ, 1, 0) < 0) {

perror("msgrcv");

exit(1);

printf("%s\n", rbuf.mtext);

exit(0);

10
OUTPUT:

11
WEEK-3

Write a program to create an integer variable using shared memory concept and increment
the variable simultaneously by two processes. Use semaphores to avoid the race conditions

PROGRAM:

HEADER:

#include<stdlib.h>

#include<stdio.h>

#include<sys/ipc.h>

#include<sys/sem.h>

#include<sys/shm.h>

SERVER:

#include "header.h"

int main()

int sid,mid, i;

char ch;

int *ptr;

struct sembuf s;

s.sem_num = 0;

s.sem_op = -1;

s.sem_flg = SEM_UNDO;

sid = semget(1234, 1, 0666|IPC_CREAT);

mid = shmget(5678, 10, 0666|IPC_CREAT);

ptr = (int *)shmat(mid, NULL, 0);

*ptr = 00;

12
semctl(sid, 0, SETVAL, 1));

for(i=0; i<10; i++)

semop(sid, &s, 1);

*ptr = (*ptr) + 1;

printf("\n server changed value to %d", *ptr);

sleep(5);

s.sem_op = 1;

semop(sid, &s, 1);

CLIENT:

#include "header.h"

int main()

int sid,mid, i;

char ch;

int *ptr;

struct sembuf s;

s.sem_num = 0;

s.sem_op = -1;

s.sem_flg = SEM_UNDO;

sid = semget(1234, 0, 0);

mid = shmget(5678, 10, 0);

ptr = (int *)shmat(mid, NULL, 0);

semctl(sid, 0, GETVAL));

13
for(i=0; i<10; i++)

semop(sid, &s, 1);

*ptr = (*ptr) + 1;

printf("\n client changed value to %d", *ptr);

sleep(5);

s.sem_op = 1;

semop(sid, &s, 1);

14
OUTPUT:

15
WEEK- 4 & 5

Design TCP iterative client and server application to reverse the given input sentence

PROGRAM:

HEADER:

#include <sys/types.h>

#include <sys/socket.h>

#include <stdio.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h>

CLIENT:

#include "header.h"

int main(int argc, char *argv[])

int s,sal;

struct sockaddr_in sa;

char buf[20];

if (argc != 2)

fprintf (stderr, "usage: program-name some-string \n");

exit(1);

16
}

s = socket(AF_INET, SOCK_STREAM, 0);

sa.sin_family = AF_INET;

sa.sin_addr.s_addr = inet_addr("127.0.0.1");

sa.sin_port = htons(9734);

sal = sizeof(sa);

connect(s, (struct sockaddr *)&sa, sal);

write(s, argv[1], strlen(argv[1]));

int n = read(s, buf, 20);

buf[n] ='\0';

printf("\nString from server = %s \n", buf);

close(s);

exit(0);

SERVER:

#include "header.h"

int main()

int ss, cs;

int sal, cal;

struct sockaddr_in sa,ca;

char buf[20];

ss = socket(AF_INET, SOCK_STREAM, 0);

sa.sin_family = AF_INET;

17
sa.sin_addr.s_addr = htonl(INADDR_ANY);

sa.sin_port = htons (9734);

sal = sizeof(sa);

bind(ss, (struct sockaddr *)&sa, sal);

listen(ss, 5);

cal = sizeof(ca);

while(1) {

cs = accept(ss, (struct sockaddr *)&ca, &cal);

int n = read(cs, buf,20);

buf[n] = '\0';

int i,j;

for(i=0,j=strlen(buf)-1; i<j; i++,j--)

{char t=buf[i]; buf[i]=buf[j]; buf[j]=t;}

write (cs, buf, n);

close (cs);

18
OUTPUT:

19
WEEK-6

Design TCP client and server application to transfer file

PROGRAM:

HEADER:

#include <sys/socket.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <stdio.h>

#include <fcntl.h>

CLIENT:

#include "header.h"

#define PORT 2080

main()

int s = socket(AF_INET,SOCK_STREAM,0);

struct sockaddr_in sa;

sa.sin_port = htons(PORT);

sa.sin_family = AF_INET;

sa.sin_addr.s_addr = inet_addr("127.0.0.1");

connect(s, (struct sockaddr *)&sa, sizeof(sa));

int fd = creat("client.txt", 0666);

char buf[50];

int n;

while(( n =read(s,buf,50))>0 )

{write( fd, buf, n);}

20
close(fd);

close(s);

SERVER:

#include "header.h"

#define PORT 2080

main(){

int ss,cs;

ss = socket(AF_INET,SOCK_STREAM,0);

struct sockaddr_in sa,ca;

sa.sin_family = AF_INET;

sa.sin_port = htons(PORT);

sa.sin_addr.s_addr = inet_addr("127.0.0.1");

bind(ss,(struct sockaddr *)&sa, sizeof(sa));

listen(ss,5);

int cal = sizeof(ca);

cs=accept(ss,(struct sockaddr *)&ca, &cal);

printf("\n Client Connected\n");

int fd = open("server.txt",O_RDONLY,0);

char buf[50];

int n;

while((n = read(fd,buf,50)) > 0)

{write(cs,buf,n);}

close(fd);

return 0;

21
OUTPUT:

22
WEEK-9

HEADER:

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#include <sys/wait.h>

#include <netdb.h>

SERVER:

#include "week9h.h"

#define MYPORT 4950

main()

struct sockaddr_in sa, ca;

int addr_len, n;

char buf[20];

int ss = socket(AF_INET, SOCK_DGRAM, 0);

sa.sin_family = AF_INET;

sa.sin_port = htons(MYPORT);

sa.sin_addr.s_addr = INADDR_ANY;

bzero(&(sa.sin_zero), 8);

int sal = sizeof(struct sockaddr);

23
bind(ss, (struct sockaddr *)&sa,sal );

while(1){

n=recvfrom( ss, buf, 20, 0, (struct sockaddr *)&ca, &sal);

buf[n] = '\0';

printf("client sent... \"%s\"\n",buf);

int i,j;

for( i=0, j=strlen(buf)-1; i<j; i++,j--)

{ char t= buf[i]; buf[i]= buf[j]; buf[j]= t; }

n=sendto(ss, buf, strlen(buf), 0,(struct sockaddr *)&ca, sal);

close(ss); }

CLIENT:

#include "week9h.h"

#define MYPORT 4950

int main(int argc, char *argv[])

struct sockaddr_in sa;

int n;

if (argc != 2) {

fprintf(stderr,"usage:progamname somestring \n");

exit(1);}

int s = socket(AF_INET, SOCK_DGRAM, 0);

sa.sin_family = AF_INET;

sa.sin_port = htons(MYPORT);

sa.sin_addr.s_addr = inet_addr("127.0.0.1");

24
bzero(&(sa.sin_zero), 8);

n =sendto(s, argv[1], strlen(argv[1]), 0,(struct sockaddr *)&sa, sizeof(struct sockaddr));

char buf[20];

int sal = sizeof(sa);

n=recvfrom(s, &buf, 20, 0, (struct sockaddr *)& sa, &sal);

buf[n] = '\0';

printf("\nserver sent back... \"%s\"\n",buf);

close(s);

return 0;

25
OUTPUT:

26
WEEK-10

HEADER:

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#include <sys/wait.h>

#include <netdb.h>

#include <fcntl.h>

SERVER:

#include "week10h.h"

#define MYPORT 3636

int main()

struct sockaddr_in sa;

int n;

int s=socket(AF_INET, SOCK_DGRAM, 0);

sa.sin_family = AF_INET;

sa.sin_port = htons(MYPORT);

sa.sin_addr.s_addr = inet_addr("127.0.0.1");

bzero(&(sa.sin_zero), 8);

27
char fname[] = "aaaa";

int fd = creat("client.txt", 0666);

char buf[20];

int len = sizeof(sa);

while(1){

sendto(s, fname, 4 , 0,(struct sockaddr *)&sa,len );

n=recvfrom(s, buf, 20, 0, (struct sockaddr *)& sa, &len);

if( n == 0 ) { break; }

write(fd, buf, n); }

close(fd); close(s);

return 0;

CLIENT:

#include "week10h.h"

#define MYPORT 3636

main()

struct sockaddr_in sa, ca;

int cal; char fname[10];

int ss=socket(AF_INET, SOCK_DGRAM, 0);

sa.sin_family = AF_INET;

sa.sin_port = htons(MYPORT);

sa.sin_addr.s_addr = INADDR_ANY;

bzero(&(sa.sin_zero), 8);

28
bind(ss, (struct sockaddr *)&sa, sizeof(struct sockaddr));

cal = sizeof(struct sockaddr);

char buf[20];

int fd = open("server.txt",O_RDONLY, 0);

int n;

while(1){

recvfrom( ss, fname, 10, 0, (struct sockaddr *)&ca, &cal);

n = read(fd,buf,20);

sendto(ss, buf, n, 0,(struct sockaddr *)&ca, cal);

if(n == 0) { break ; }}

close(fd); close(ss);

return 0;

29
OUTPUT:

30

You might also like