Week1.
Implement the following forms of IPC.
a) Pipes b) FIFO
a) Named Pipes
Half Duplex
---------------------------------------------------------------------------------------------------------------------
half Duplex.h
#define HALF_DUPLEX "/tmp/halfduplex"
#define MAX_BUF_SIZE 255
---------------------------------------------------------------------------------------------------------------------
hd_server.c
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "half_duplex.h" /* For name of the named-pipe
*/
#include <stdlib.h>
int main(int argc, char *argv[])
{
int fd, ret_val, count, numread;
char buf[MAX_BUF_SIZE];
/* Create the named - pipe */
ret_val = mkfifo(HALF_DUPLEX, 0666);
if ((ret_val == -1) && (errno != EEXIST)) {
perror("Error creating the named pipe\n");
exit (1);
}
/* Open the pipe for reading */
fd = open(HALF_DUPLEX, O_RDONLY);
/* Read from the pipe */
numread = read(fd, buf, MAX_BUF_SIZE);
4
buf[numread] = '0';
printf("Half Duplex Server : Read From the pipe : %s\n",
buf);
/* Convert to the string to upper case */
count = 0;
while (count < numread) {
buf[count] = toupper(buf[count]);
count++;
}
printf("Half Duplex Server : Converted String : %s\n",
buf);
}
---------------------------------------------------------------------------------------------------------------------
halfduplex1.h
#define HALF_DUPLEX "/tmp/halfduplex1"
#define MAX_BUF_SIZE 255
---------------------------------------------------------------------------------------------------------------------
hd_client.c
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include "half_duplex1.h" /* For name of the named-pipe
*/
#include <stdlib.h>
int main(int argc, char *argv[])
{
int fd;
/* Check if an argument was specified. */
if (argc != 2) {
printf("Usage : %s <string to be sent to the
server>n", argv[0]);
exit (1);
}
5
/* Open the pipe for writing */
fd = open(HALF_DUPLEX, O_WRONLY);
/* Write to the pipe */
write(fd, argv[1], strlen(argv[1]));
}
Execution Steps:
1. Named Pipes:
a) Half Duplex.
1. Run the server:
% cc hd_server.c
% mv a.out hd_server
%./hd_server &
The server program will block here, and the shell will return
control to the
command line.
2. Run the client:
% cc hd_client
% mv a.out hd_client
%./hd_client
hello
3. The server prints the string read and terminates:
Output:-
Half Duplex Server : Read From the pipe : hello
Half Duplex Server : Converted String : HELLO
---------------------------------------------------------------------------------------------------------------------
6
b) Named Pipe:
Full Duplex:
full duplex.h
#define NP1 "/tmp/np1"
#define NP2 "/tmp/np2"
#define MAX_BUF_SIZE 255
---------------------------------------------------------------------------------------------------------------------
fd_server.c
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "fullduplex.h" /* For name of the named-pipe */
#include <stdlib.h>
#include<string.h>
int main(int argc, char *argv[])
{
int rdfd, wrfd, ret_val, count, numread;
char buf[MAX_BUF_SIZE];
/* Create the first named - pipe */
ret_val = mkfifo(NP1, 0666);
if ((ret_val == -1) && (errno != EEXIST)) {
perror("Error creating the named pipe");
exit (1);
}
ret_val = mkfifo(NP2, 0666);
if ((ret_val == -1) && (errno != EEXIST)) {
perror("Error creating the named pipe");
exit (1);
}
/* Open the first named pipe for reading */
rdfd = open(NP1, O_RDONLY);
7
/* Open the second named pipe for writing */
wrfd = open(NP2, O_WRONLY);
/* Read from the first pipe */
numread = read(rdfd, buf, MAX_BUF_SIZE);
buf[numread] = '0';
printf("Full Duplex Server : Read From the pipe : %s\n",
buf);
/* Convert to the string to upper case */
count = 0;
while (count < numread) {
buf[count] = toupper(buf[count]);
count++;
}
/*
* * Write the converted string back to the second
* * pipe
* */
write(wrfd, buf, strlen(buf));
}
---------------------------------------------------------------------------------------------------------------------
8
fd_client.c
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "fullduplex.h" /* For name of the named-pipe */
#include <stdlib.h>
#include<string.h>
int main(int argc, char *argv[])
{
int wrfd, rdfd, numread;
char rdbuf[MAX_BUF_SIZE];
/* Check if an argument was specified. */
if (argc != 2) {
printf("Usage : %s <string to be sent to the
server>n", argv[0]);
exit (1);
}
/* Open the first named pipe for writing */
wrfd = open(NP1, O_WRONLY);
/* Open the second named pipe for reading */
rdfd = open(NP2, O_RDONLY);
/* Write to the pipe */
write(wrfd, argv[1], strlen(argv[1]));
/* Read from the pipe */
numread = read(rdfd, rdbuf, MAX_BUF_SIZE);
rdbuf[numread] = '0';
printf("Full Duplex Client : Read From the Pipe : %s\n",
rdbuf);
}
9
Execution Steps:
b) Full Duplex.
1. Run the server:
% cc fd_server.c
% mv a.out fd_server
%./fd_server &
The server program will block here, and the shell will return
control to the
command line.
2. Run the client:
% cc fd_client
% mv a.out fd_client
%./fd_client
hello
3. The client program will send the string to server and block on
the read
to await the server's response.
4. The server prints the following:
Full Duplex Server : Read From the pipe : hello
The client prints the following:
Full Duplex Client : Read From the pipe : HELLO
---------------------------------------------------------------------------------------------------------------------
10
Week2.
Implement file transfer using Message Queue form of IPC
message_send.c -- creating and sending to a simple message queue
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#define MSGSZ 128
/*
* Declare the message structure.
*/
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;
/*
* Get the message queue id for the
* "name" 1234, which was created by
* the server.
*/
key = 1234;
(void) fprintf(stderr, "\nmsgget: Calling msgget(%#lx,\
%#o)\n",
key, msgflg);
if ((msqid = msgget(key, msgflg )) < 0) {
perror("msgget");
11
exit(1);
}
else
(void) fprintf(stderr,"msgget: msgget succeeded: msqid =
%d\n", msqid);
/*
* We'll send message type 1
*/
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 ;
/*
* Send a message.
*/
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);
}
---------------------------------------------------------------------------------------------------------------------
12
message_rec.c -- receiving the above message
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#define MSGSZ 128
/*
* Declare the message structure.
*/
typedef struct msgbuf {
long mtype;
char mtext[MSGSZ];
} message_buf;
main()
{
int msqid;
key_t key;
message_buf rbuf;
/*
* Get the message queue id for the
* "name" 1234, which was created by
* the server.
*/
key = 1234;
if ((msqid = msgget(key, 0666)) < 0) {
perror("msgget");
exit(1);
}
/*
* Receive an answer of message type 1.
*/
if (msgrcv(msqid, &rbuf, MSGSZ, 1, 0) < 0) {
perror("msgrcv");
13
exit(1);
}
/*
* Print the answer.
*/
printf("%s\n", rbuf.mtext);
exit(0);
}
14
Execution Steps:
[sampath@localhost msgque]cc message_send.c
[sampath@localhost msgque]mv a.out msgsend
[sampath@localhost msgque]$ ./msgsend
msgget: Calling msgget(0x4d2,01666)
msgget: msgget succeeded: msqid = 0
msgget: msgget succeeded: msqid = 0
msgget: msgget succeeded: msqid = 0
Message: "Did you get this?" Sent
[sampath@localhost msgque]cc message_rec.c
[sampath@localhost msgque]mv a.out msgrec
[sampath@localhost msgque]$ ./msgrec &
[1] 2907
[sampath@localhost msgque]$ Did you get this?
---------------------------------------------------------------------------------------------------------------------
15
Week3.
Write a program to create an integer variable using shared memory
concept and increment
the variable simultaneously by two processes. Use semaphores to
avoid race conditions
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(void) {
pid_t pid;
int *shared; /* pointer to the shm */
int shmid;
shmid = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666);
printf("Shared Memory ID=%u",shmid);
if (fork() == 0) { /* Child */
/* Attach to shared memory and print the pointer */
shared = shmat(shmid, (void *) 0, 0);
printf("Child pointer %u\n", shared);
*shared=1;
printf("Child value=%d\n", *shared);
sleep(2);
printf("Child value=%d\n", *shared);
} else { /* Parent */
/* Attach to shared memory and print the pointer */
shared = shmat(shmid, (void *) 0, 0);
printf("Parent pointer %u\n", shared);
printf("Parent value=%d\n", *shared);
sleep(1);
*shared=42;
printf("Parent value=%d\n", *shared);
sleep(5);
shmctl(shmid, IPC_RMID, 0);
}
}
16
Execution steps:
[sampath@localhost ipc]$cc shared_mem.c
[sampath@localhost ipc]$ ./a.out
Shared Memory ID=65537Child pointer 3086680064
Child value=1
Shared Memory ID=65537Parent pointer 3086680064
Parent value=1
Parent value=42
Child value=42
Hill Cipher
#include<stdio.h>
int main()
{
int i,j,ans[25][1],sum=0,mtrx[25][25],end;
char txt[25];
printf("\nEnter the string : ");
scanf("%s",txt);
for(i=0;i<25;i++)
{
if(txt[i]>=97 && txt[i]<122) {}
else
{
end=i;
break;
}
}
for(i=0;i<end;i++)
{
printf("initial : %d ",txt[i]);
txt[i]=txt[i]-'a';
printf("final : %d ",txt[i]);
printf("\n\n");
}
printf("\nEnter matrix...\n");
for(i=0;i<end;i++)
{
for(j=0;j<end;j++)
{
scanf("%d",&mtrx[i][j]);
}
}
for(i=0;i<end;i++)
{
sum=0;
for(j=0;j<end;j++)
{
sum+=mtrx[i][j]*(int)txt[j];
}
ans[i][0]=sum;
}
for(i=0;i<end;i++)
{
printf(" %c",((ans[i][0])%26)+97);
}
}
int main()
{
int i,j,ans[25][1],sum=0,mtrx[25][25],end;
char txt[25];
printf("\nEnter the string : ");
scanf("%s",txt);
for(i=0;i<25;i++)
{
if(txt[i]>=97 && txt[i]<122) {}
else
{
end=i;
break;
}
}
for(i=0;i<end;i++)
{
printf("initial : %d ",txt[i]);
txt[i]=txt[i]-'a';
printf("final : %d ",txt[i]);
printf("\n\n");
}
printf("\nEnter matrix...\n");
for(i=0;i<end;i++)
{
for(j=0;j<end;j++)
{
scanf("%d",&mtrx[i][j]);
}
}
for(i=0;i<end;i++)
{
sum=0;
for(j=0;j<end;j++)
{
sum+=mtrx[i][j]*(int)txt[j];
}
ans[i][0]=sum;
}
for(i=0;i<end;i++)
{
printf(" %c",((ans[i][0])%26)+97);
}
}
OUTPUT:
Better only give this as input
[1210310505@CSELABServer2 ~]$ ./a.out
Enter the string : pay
initial : 112 final : 15
initial : 112 final : 15
initial : 97 final : 0
initial : 121 final : 24
Enter matrix...
17
17
5
21
18
21
2
2
19
l n s[1210310505@CSELABServer2 ~]$
Ceaser cipher: encryption
#include<stdio.h>
#include<string.h>
main()
{
int i,n;
int j=0;
int k;
int l=0;
char a[26];
char s[100];
for(i=0;i<26;i++)
{
j=65+i;
a[i]=(char)j;
printf("%c",a[i]);
}
printf("\nenter the
nuber of letters");
scanf("%d",&n);
printf("\nenter the
text");
for(i=0;i<(n+1);i++)
{
scanf("%c",&s[i]);
printf("%c",s[i]);
}
printf("\n");
for(i=0;i<(n+1);i++)
{
for(j=0;j<26;j++)
{
if(s[i]==a[j])
{
k=(j+3)%26;
printf("%c",a[k]);
}
}
}
}
Decryption:
#include<stdio.h>
#include<string.h>
main()
{
int i,n;
int j=0;
int k;
int l=0;
char a[26];
char s[100];
for(i=0;i<26;i++)
{
j=65+i;
a[i]=(char)j;
printf("%c",a[i]);
}
printf("\nenter the
nuber of letters");
scanf("%d",&n);
printf("\nenter the
text");
for(i=0;i<(n+1);i++)
{
scanf("%c",&s[i]);
printf("%c",s[i]);
}
for(i=0;i<(n+1);i++)
{
for(j=0;j<26;j++)
{
if(s[i]==a[j])
{
k=(j-3)%26;
printf("%c",a[k]);
}
}
}
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[30],k[27],c[30];
int i, index;
clrscr();
printf("Enter plain text: ");
gets(s);
printf("\nEnter key with 26 character:");
for(i=0;i<26;i++)
{
printf("\n%c",i+97);
k[i]=getch();
printf("%c",k[i]);
}
for(i=0;i<strlen(s);i++)
{
index=s[i]-97;
c[i]=k[index];
}
printf("Your cipher text is:");
for(i=0;i<strlen(s);i++)
{
printf("%c",c[i]);
}
getch();
}
#include<conio.h>
#include<string.h>
void main()
{
char s[30],k[27],c[30];
int i, index;
clrscr();
printf("Enter plain text: ");
gets(s);
printf("\nEnter key with 26 character:");
for(i=0;i<26;i++)
{
printf("\n%c",i+97);
k[i]=getch();
printf("%c",k[i]);
}
for(i=0;i<strlen(s);i++)
{
index=s[i]-97;
c[i]=k[index];
}
printf("Your cipher text is:");
for(i=0;i<strlen(s);i++)
{
printf("%c",c[i]);
}
getch();
}