1.AIM:
Write a program to create pipes?
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#define MAX 50
main()
{
pid_t pid;
int fd[2];
char line[MAX];
if (pipe(fd)<0)
{
perror("PIPE");
exit(1);
}
pid=fork();
if (pid<0)
{
perror("FORK");
exit(1);
}
if (pid==0)
Page 1
EXECUTION STEPS:
[sampath@localhost ipc]$. vi pipes.c
[sampath@localhost ipc]$. cc pipes.c
[sampath@localhost ipc]$. ./a.out
PARENT IS ACTIVE
CHILD IS ACTIVE...
Page 2
2.AIM:
Write a program for FIFO?
PROGRAM
Page 3
do
{
if ((num = read(fd, s, 300)) == -1)
perror("read");
else
{
Page 4
return 0;
}
EXECUTION STEPS:
[sampath@localhost ipc]$. vi fifo.c
[sampath@localhost ipc]$. cc fifo.c
[sampath@localhost ipc]$. ./a.out
Page 5
3.AIM:
Write a program to create, write and read from shared memory?
PROGRAM
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
Page 6
else
{
/* Parent */ /* Attach to shared memory and print the pointer */
shared = shmat(shmid, (void *) 0, 0);
Page 7
EXECUTION STEPS:
Page 8
Page 9
4.AIM:
Write a program to implement various operations on Message Queue ?
PROGRAM
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
mtype;
char
mtext[MSGSZ];
} message_buf;
main()
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
message_buf sbuf;
Page 10
Page 11
128
mtype;
char
mtext[MSGSZ];
} message_buf;
main()
{
int msqid;
key_t key;
Page 12
Page 13
EXECUTION STEPS:
[sampath@localhost msgque]vi message_send.c
[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
AVR & SVR CET
Page 14
5.AIM:
Perform socket programming using UDP?
PROGRAM
udp_server.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
AVR & SVR CET
Page 15
Page 16
for(;;)
{
printf("I am waiting\n");
/*Received a datagram*/
length=sizeof(cliaddr);
n=recvfrom(sd,datareceived,BUFSIZE,0, (struct sockaddr*)&cliaddr ,
&length);
printf("Data Received from %s\n", net_ntop(AF_INET,&cliaddr.sin_addr,
clientname,sizeof(clientname)));
/*Sending the Received datagram back*/
datareceived[n]='\0';
printf("I have received %s\n",datareceived);
sendto(sd,datareceived,n,0,(struct sockaddr *)&cliaddr,length);
}
}
udp_client.c
#include <stdio.h>
#include <stdlib.h>
AVR & SVR CET
Page 17
Page 18
if (NULL==argv[2])
{
printf("Please enter the Port Number of the server\n");
exit(0);
}
sd = socket( AF_INET, SOCK_DGRAM, 0 );
if( sd < 0 )
{
perror( "socket" );
exit( 1 );
}
Page 19
int sockfd ,
struct sockaddr *to ,socklen_t length) /*Connection Socket */
{
int maxdes,n;
fd_set rset;
char sendbuf[BUFSIZE] , recvbuf[BUFSIZE] ,servername[100];
struct sockaddr_in serveraddr;
socklen_t slen;
FD_ZERO(&rset);
maxdes=(sockfd>fileno(fp)?sockfd+1:fileno(fp)+1);
for(;;)
{
FD_SET(fileno(fp) , &rset);
Page 20
Page 21
EXECUTION STEPS:
UDP Client Server Application.
Compiling and running server.
[user@localhost week9]$ vi udp_server.c
[user@localhost week9]$ cc udp_server.c
[user@localhost week9]$ mv a.out udp_server
[user@localhost week9]$ ./ udp_server
I am waiting
Data Received from 127.0.0.1
I have received abcd efgh
rev is
hgfe dcba
I am waiting
Compiling and running client.
user@localhost week9]$ vi udp_client.c
AVR & SVR CET
Page 22
6.AIM:
Perform socket programming using TCP ?
PROGRAM
tcpservselect01.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
AVR & SVR CET
Page 23
int
nready, client[FD_SETSIZE];
ssize_t
n;
fd_set
rset, allset;
char
line[MAXLINE],buf[100];
socklen_t
clilen;
= AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
Page 24
= htons(SERV_PORT);
/* initialize */
FD_ZERO(&allset);
FD_SET(listenfd, &allset);
/* end fig01 */ /* include fig02 */
for ( ; ; )
{
printf("Server:I am waiting-----Start of Main Loop\n");
rset = allset;
/* structure assignment */
Page 25
/* for select */
if (i > maxi)
maxi = i;
if (--nready <= 0)
continue;
}
for (i = 0; i <= maxi; i++)
{
/* check all clients for data */
if ( (sockfd = client[i]) < 0)
continue;
if (FD_ISSET(sockfd, &rset))
{
if ( (n = read(sockfd, line, MAXLINE)) == 0)
{
/*4connection closed by client */
Page 26
}
}
Page 27
Page 28
if (V[1] == NULL )
{
printf ("PL specfiy the server's IP Address \n");
exit(0);
}
if (V[2] == NULL )
{
printf ("PL specify the server's Port No \n");
exit(0);
}
memset( &serveraddress, 0, sizeof(serveraddress) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(atoi(V[2]));//PORT NO
serveraddress.sin_addr.s_addr = inet_addr(V[1]);//ADDRESS
if (connect(sd,(struct sockaddr*)&serveraddress, sizeof(serveraddress))<0)
{
printf("Cannot Connect to server");
exit(1);
}
printf("enter sentence to end enter #");
while(1)
{
Page 29
text[i]='\0';
sendstring(sd,text);
close(sd);
return 0;
}
void sendstring ( int sd, char *fname) /*Socket Descriptor*/ /*Array Containing the
string */
{
int n , byteswritten=0 , written ;
char buffer[MAXBUFFER];
strcpy(buffer , fname);
n=strlen(buffer);
while (byteswritten<n)
{
written=write(sd , buffer+byteswritten,(n-byteswritten));
byteswritten+=written;
}
printf("String : %s sent to server \n",buffer);
}
Page 30
EXECUTION STEPS:
b) Concurrent Server Application Using Select.
Compiling and running server.
root@localhost week7and8]# vi tcpservselect01.c
root@localhost week7and8]# cc tcpservselect01.c
[root@localhost week7and8]# mv a.out tcpservselect1
[root@localhost week7and8]# ./tcpservselect1
Server:I am waiting-----Start of Main Loop
Server:I am waiting-----Start of Main Loop
output at server
abcd
Page 31
7.AIM:
Write a program for process creation and process execution for display environment
variables.
PROGRAM
#include<stdio.h>
int main(int argc, char *argv[],char *env[])
{
int i;
printf(" my os environment: \n");
for(i=0;env[i];i++)
printf("%s\n", env[i]);
return 0;
AVR & SVR CET
Page 32
#include<stdio.h>
extern char ** environ;
int main()
{
int i;
printf(" my os environment\n");
for(i=0;environ[i];i++)
printf("%s\n",environ[i]);
return 0;
}
Page 33
EXECUTION STEPS:
[sampath@localhost ipc]$. vi env1.c
[sampath@localhost ipc]$. cc env1.c
[sampath@localhost ipc]$. ./a.out
my os environment
my os environment
Page 34
8. AIM:
Write a program on process creation and execution for implement different types of execution
function
PROGRAM
Page 35
#include<unistd.h>
int main()
{
execl("./hello","1",(char *)0);
printf(" display read line variables\n");
}
Page 36
EXECUTION STEPS:
[sampath@localhost ipc]$. vi env1.c
[sampath@localhost ipc]$. cc env1.c
[sampath@localhost ipc]$. ./a.out
Page 37
Page 38
9. AIM:
Write a program to create file and modify and to delete record
PROGRAM
#include<stdio.h>
int main(int argc, char *argv[])
{
char ch;
FILE *fp;
fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("file does not exit.\n");
return 0;
Page 39
Page 40
EXECUTION STEPS:
[sampath@localhost ipc]$. vi file.c
[sampath@localhost ipc]$. cc file.c
[sampath@localhost ipc]$. ./a.out
Page 41
Page 42
10.AIM:
Write a program to find status and mode value of a file
PROGRAM
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
char data[1];
char str[1024];
int main(int argc,char *argv[])
{
int count;
int ch;
int fd;
int pos,len;
if(argc!=2)
{
printf("command requires two arguments./n");
exit(1);
}
fd=open(argv[1],O_RDWR);
Page 43
Page 44
while(read(fd,data,1)>0)
{
count++;
write(1,data,1);
if(count==len)break;
}
break;
case 2 :
printf("Enter the position to write:");
scanf("%d",&pos);
printf("Enter the string towrite into to file:");
scanf("%s",str);
lseek(fd,pos-1,SEEK_SET);
write(fd,str,strlen(str));
break;
case 3 :
exit(0);
default:
printf("Invalid choice. Try again./n");
}
}while(1);
exit(0);
}
Page 45
Page 46
Page 47
11. AIM:
Write a program to find whether a file is having read , write , execute permissions and also
check whether the given name is file or directory
PROGRAM
#include<stdio.h>
AVR & SVR CET
Page 48
Page 49
Page 50
12. AIM:
Write a program to create a fork function and to display child process id , parent process id
,root id
Page 51
#include<stdio.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
int i,pid,value;
sscanf(argv[1],"%d",&value);
for (i=0;i<value-1;i++)
{
pid=fork();
if(pid==0)
{
printf("I'm child My pid=%d\t",getpid());
printf("My parent pid=%d\t ",getppid());
printf("My Groupid=%d\n ",getgid());
}
else
wait();
}
return 0;
}
Page 52
EXECUTION STEPS:
[sampath@localhost ipc]$. vi fork.c
[sampath@localhost ipc]$. cc fork.c
[sampath@localhost ipc]$. ./a.out
Page 53
Page 54