Unix Lab Manual
Unix Lab Manual
Unix Lab Manual
/************************************************************************
1(b)Aim:A Program to implement different types of exec functions
Branch:III year CSE
*************************************************************************/
#include<stdio.h>
main()
{
int r;
char *cmd[]={"ls","-t",(char *)0};
char *env[]={"home=/user/home","logname=home",(char *)0};
char *argv[]={"/bin/ls","-t","-l",(char *)0};
execl("/bin/ls","-r","-t","-l",(char *)0);
execv("/bin/ls","-r","-t","-l",(char *)0);
execle("/bin/ls","ls","-l",(char *)0);
r=execlp("ls","ls","-l",(char *)0);
r=execve("/bin/ls",cmd,env);
r=execvp("ls",cmd);
}
/***********************************************************************
output
aits@ubuntu:~$ vi different.c
aits@ubuntu:~$ cc different.c
aits@ubuntu:~$ ./a.out
total 20
-rw-rw-r-- 1 aits aits 700 Mar 4 14:48 different.c
-rwxrwxr-x 1 aits aits 7353 Mar 4 14:35 a.out
-rw-rw-r-- 1 aits aits 3103 Feb 25 16:36 display.c
-rw-rw-r-- 1 aits aits 422 Feb 25 16:35 display.c~
***********************************************************************/
/************************************************************************
2(a)Aim:A Program to create chain processes
Branch:III year CSE
*************************************************************************/
#include<stdio.h>
#include<fcntl.h>
int main()
{
fork();
fork();
fork();
printf("\nHELLO");
}
/*************************************************************************
output
aits@ubuntu:~$ vi chain.c
aits@ubuntu:~$ cc chain.c
aits@ubuntu:~$ ./a.out
HELLO
HELLO
aits@ubuntu:~$ HELLO
HELLO
HELLO
HELLO
HELLO
HELLO
**************************************************************************/
/************************************************************************
2(b)Aim:A Program to create chain processes and print the process id's
Branch:III year, CSE
*************************************************************************/
#include<stdio.h>
#include<fcntl.h>
int main()
{
if(fork()==0)
{
printf("%d%d\n",getpid(),getppid());
if(fork()==0)
{
printf("%d%d\n",getpid(),getppid());
if(fork()==0)
printf("%d%dhello()\n",getpid(),getppid());
}
}
return 0;
}
/*************************************************************************
output
aits@ubuntu:~$ vi process.c
aits@ubuntu:~$ cc process.c
aits@ubuntu:~$ ./a.out
aits@ubuntu:~$ 28392838
28402839
28412840hello()
***************************************************************************/
/****************************************************************************
2(c)Aim: A Program to create chain processes and print the process id's
along with parent id's
Branch:III year CSE
****************************************************************************/
#include<stdio.h>
#include<fcntl.h>
int main()
{
fork();
printf("%d%d",getpid(),getppid());
fork();
printf("\nHELLO");
fork();
printf("\nHAI");
}
/****************************************************************************
output
aits@ubuntu:~$ vi pid.c
aits@ubuntu:~$ cc pid.c
aits@ubuntu:~$ ./a.out
29202356
HELLO
HAI29202356
HELLO
HAIHELLO
HAIHELLO
HAIaits@ubuntu:~$ 29211
HELLO
HAI29211
HELLO
HAIHELLO
HAIHELLO
HAI
****************************************************************************/
/****************************************************************************
2(d)Aim: A Program to create chain processesto calculate the sum of odd
numbers and even numbers
Branch:III year CSE
****************************************************************************/
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{
int i,n,pid,tpid,sum=0;
pid-tpid;
system("clear");
printf("\nenter n value");
scanf("%d",&n);
pid=fork();
if(pid==0)
{
printf("from chid process\n");
for(i=1;i<n;i+=2)
{
printf("\nlist of odd sum");
printf("%d",i);
sum+=i;
}
printf("odd sum=%d\n",sum);
}
else
{
printf("from process\n");
for(i=0;i<n;i+=2)
{
printf("\nlist of event sum");
printf("%d\n",i);
sum+=i;
}
printf("even sum=%d\n",sum);
}
}
/****************************************************************************
output
aits@ubuntu:~/534$ vi even.c
aits@ubuntu:~/534$ cc even.c
aits@ubuntu:~/534$ ./a.out
enter n value7
from process
list of event sum0
list of event sum2
list of event sum4
list of event sum6
even sum=12
from chid process
list of odd sum1
list of odd sum3
list of odd sum5odd sum=9
****************************************************************************/
/************************************************************
3(a)Aim:write a program to open a stream (or) file
Branch:III year CSE
*************************************************************/
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
main()
{
int fd;
fd=creat("ab.text",0666);
if(fd==-1)
printf("\nfile cant be created");
else
printf("\nfile is created");
fd=open("ab.text",O_RDONLY);
if(fd==-1)
printf("\nfile cant be opened");
else
printf("\nfile opened");
return 0;
}
/*************************************************************
output
aits@ubuntu:~$ cc stream.c
aits@ubuntu:~$ ./a.out
file is created
file opened
**************************************************************/
/*******************************************************
3(b) Aim:A Program to read a stream and write a stream
Branch:III year CSE
**************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
char buffer[2048];
main(argc,argv)
int argc;
char *argv[];
{
int fd1,fd2,n;
fd1=open("terminal.c",O_RDONLY);
if(fd1==-1)
printf("file cant open");
else
printf("file opened\n");
fd2=creat("rdwrop.c",0666);
if(fd2==-1)
printf("file cant create");
else
printf("file created\n");
while(read(fd1,buffer,sizeof(buffer))>0)
write(fd2,buffer,sizeof(buffer));
}
/********************************************************
Output
aits@ubuntu:~/534$ vi rstream.c
aits@ubuntu:~/534$ cc rstream.c
aits@ubuntu:~/534$ ./a.out
file opened
file created
**********************************************************/
/************************************************************
3(c) Aim :A Program to position the file
Branch:III year CSE
**************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
char buffer[2048];
main(argc,argv)
char *argv[];
{
int fd,var,count=0;
char c;
fd=open("terminal.c",O_RDONLY);
while((var=read(fd,&c,1))>0&&(count<10))
{
printf("%c",c);
var=lseek(fd,0L,1);
printf("var=%d\n",var);
count++;
}
return 0;
}
/***********************************************************
Output
aits@ubuntu:~$ vi position.c
aits@ubuntu:~$ cc position.c
aits@ubuntu:~$ ./a.out
jvar=1
svar=2
fvar=3
jvar=4
avar=5
fvar=6
evar=7
jvar=8
fvar=9
gvar=10
************************************************************/
/**************************************************************
4(a) Aim:write a program for one way communication where child process sends
a message to a parent process by using single pipe
Branch:III year CSE
**************************************************************/
#include<stdio.h>
main()
{
#define MSGSIZE 16
char *msg1="hello world";
char inbuff[MSGSIZE];
int p[2],pid;
pipe(p);
pid=fork();
if(pid==0)
{
write(p[1],msg1,MSGSIZE);
}
else
{
sleep(2);
printf("i am in parent process\n");
read(p[0],inbuff,MSGSIZE);
printf("\nmsg is:");
printf("%s",inbuff);
}
}
/***********************************************************
Output
aits@ubuntu:~$ vi child.c
aits@ubuntu:~$ cc child.c
aits@ubuntu:~$ ./a.out
i am in parent process
msg is:hello world
***********************************************************/
/************************************************************
4(b) Aim:Write a program for one way communication where parent send a
message to child process by using single pipe
Branch:III year CSE
**************************************************************/
#include<stdio.h>
#define MSGSIZE 16
main()
{
char *msg1="hello child";
char inbuff[MSGSIZE];
int p[2],pid;
pipe(p);
pid=fork();
if(pid==0)
{
sleep(2);
printf("i am in child process");
read(p[0],inbuff,MSGSIZE);
printf("the msg is:");
printf("%s",inbuff);
}
else
{
write(p[1],msg1,MSGSIZE);
}
}
/**********************************************************
Output
aits@ubuntu:~$ vi parent.c
aits@ubuntu:~$ cc parent.c
aits@ubuntu:~$ ./a.out
aits@ubuntu:~$ i am in child processthe msg is:hello child
***********************************************************/
/************************************************************
5(a)Aim:write two way communication between the parent and child process by
using two pipes where parent process initiates the communication
Branch:III year CSE
*************************************************************/
#include<stdio.h>
void main()
{
int pp[2],pc[2],pid,pipe(),close();
char msg1[20];
char msg2[20];
char msg3[20];
pipe(pp);
pipe(pc);
pid=fork();
if(pid==0)
{
close(pp[1]);
close(pc[0]);
printf("the msg received from parent is");
read(pp[0],msg1,15);
printf("%s",msg1);
write(pc[1],"hello dad\n",15);
read(pp[0],msg3,15);
printf("%s",msg3);
}
else
{
close(pp[0]);
close(pc[1]);
write(pp[1],"hello child\n",15);
read(pc[0],msg2,15);
printf("the msg received from child is");
printf("%s",msg2);
write(pp[1],"thank you child\n",25);
}
}
/*************************************************************
output
aits@ubuntu:~$ vi twowaycomm.c
aits@ubuntu:~$ cc twowaycomm.c
aits@ubuntu:~$ ./a.out
the msg received from parent is hello child
the msg received from child is hello dad
thank you child
**************************************************************/
/************************************************************
5(b)Aim:write two way communication between the parent and child process by
using two pipes where child process initiates the communication
Branch:III year CSE
*************************************************************/
#include<stdio.h>
void main()
{
int pp[2],pc[2],pid,pipe(),close();
char msg1[20];
char msg2[20];
char msg3[20];
pipe(pp);
pipe(pc);
pid=fork();
if(pid==0)
{
close(pp[0]);
close(pc[1]);
write(pp[1],"hello dad\n",15);
read(pc[0],msg2,15);
printf("the msg received from parent is");
printf("%s",msg2);
write(pp[1],"thank you dad\n",25);
}
else
{
close(pp[1]);
close(pc[0]);
read(pp[0],msg1,15);
printf("the msg received from child is");
printf("%s",msg1);
write(pc[1],"hello child\n",15);
read(pp[0],msg3,15);
printf("%s",msg3);
}
}
/*************************************************************
output
aits@ubuntu:~$ vi twowaycomm1.c
aits@ubuntu:~$ cc twowaycomm1.c
aits@ubuntu:~$ ./a.out
the msg received from child is hello dad
the msg received from parent is hello child
thank you dad
**************************************************************/
/************************************************************
6:Aim:write a program to create integer variable using semaphores
Branch:III year CSE
**************************************************************/
#include<stdio.h>
#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;
int shmid;
shmid=shmget(IPC_PRIVATE,sizeof(int),IPC_CREAT/0666);
printf("\n shared memory id is %d",shmid);
if(fork()==0)
{
shared=shmat(shmid,(void*)0,0);
printf("\n child pointer=%u",shared);
*shared=1;
printf("\n child value=%d",*shared);
sleep(2);
printf("\n child value=%d",*shared);
}
else
{
shared=shmat(shmid,(void*)0,0);
printf("\n parent pointer=%u",shared);
sleep(1);
printf("\n parent value=%d",*shared);
sleep(1);
*shared=42;
printf("\n parent value=%d",*shared);
sleep(5);
shmctl(shared,IPC_RMID,0);
}
}
/**********************************************************
Output
aits@ubuntu:~$ shared memory id is 32769
aits@ubuntu:~$ shared memory id is 32769
aits@ubuntu:~$ child pointer is: 3077537792
aits@ubuntu:~$ parent pointer is: 3077537792
parent value = 1
child value = 1
child value = 42
parent value = 42
************************************************************/
/************************************************************
7:Aim:write a program to implement for file transfering using message queues
Branch:III year CSE
**************************************************************/
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<string.h>
#include<sys/msg.h>
struct msgbuf
{
long mtype;
char mtext[1];
};
int main(int argc,char argv[])
{
int queue_id_rollno=msgget(IPC_PRIVATE,0666);
struct msgbuf *snd_msg;
struct msgbuf *recv_msg;
int rc,snd;
if(queue_id_rollno==-1)
{
perror("main():message get error");
}
printf("message queue created,queued '%d'\n",queue_id_rollno);
snd_msg=(struct msgbuf*)malloc(sizeof(struct msgbuf)+strlen("enter your
message send"));
snd_msg->mtype=1;
strcpy(snd_msg->mtext,"hello world");
snd=msgsnd(queue_id_rollno,snd_msg,strlen(snd_msg->mtext)+1,0);
if(snd==-1)
{
perror("main():message send error");
exit(1);
}
free(snd_msg);
printf("message placed on queue successfully\n");
recv_msg=(struct msgbuf*)malloc(sizeof(struct msgbuf)+strlen("enter
your msg receive"));
rc=msgrcv(queue_id_rollno,recv_msg,strlen("enter your message to
send")+1,0,0);
if(rc==-1)
{
perror("main():message receive error");
exit(1);
}
printf("msgrcv:msg received in mtype=%d,mtext='%s'\n",recv_msg>mtype,recv_msg->mtext);
return 0;
}
/**********************************************************
Output
aits@ubuntu:~$ vi message.c
aits@ubuntu:~$ cc message.c
aits@ubuntu:~$ ./a.out
message queue created,queued '131076'
message placed on queue successfully
msgrcv:msg received in mtype=1,mtext='hello world'
************************************************************/