Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

1(a)Aim:A Program for display unix environment variables

Branch:III year CSE


*****************************************************************************
****/
#include<stdio.h>
main(argc,argv,envp)
int argc;
char *argv;
char *envp[];
{
int i;
for(i=0;envp[i]!=(char *)0;i++)
printf("%s",envp[i]);
}
/****************************************************************************
output
aits@ubuntu:~$ vi display.c
aits@ubuntu:~$ cc display.c
aits@ubuntu:~$ ./a.out
SSH_AGENT_PID=1405GPG_AGENT_INFO=/tmp/keyringHrR3dS/gpg:0:1TERM=xtermSHELL=/bin/bashXDG_SESSION_COOKIE=9f469d6cab42bfff79c
d4a3d00000007-1393318574.6054051361169380WINDOWID=54525958GNOME_KEYRING_CONTROL=/tmp/keyringHrR3dSUSER=aitsLS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01
;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:o
w=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.l
zh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31
:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;
31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.
ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=0
1;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:
*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tif
f=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01
;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35
:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wm
v=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;
35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cg
m=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;
36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*
.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=0
0;36:*.xspf=00;36:XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0XD
G_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0SSH_AUTH_SOCK=/tmp/keyringHrR3dS/sshSESSION_MANAGER=local/ubuntu:@/tmp/.ICEunix/1370,unix/ubuntu:/tmp/.ICEunix/1370DEFAULTS_PATH=/usr/share/gconf/ubuntu.default.pathXDG_CONFIG_DIRS=/e
tc/xdg/xdgubuntu:/etc/xdgPATH=/home/aits/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/
usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/gamesDESKTOP_SESSION=ubuntuP
WD=/home/aits/534GNOME_KEYRING_PID=1359LANG=en_US.UTF8MANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.pathUBUNTU_MENUPROXY=libapp
menu.soCOMPIZ_CONFIG_PROFILE=ubuntuGDMSESSION=ubuntuSHLVL=1HOME=/home/aitsGNO
ME_DESKTOP_SESSION_ID=this-isdeprecatedLOGNAME=aitsXDG_DATA_DIRS=/usr/share/ubuntu:/usr/share/gnome:/usr/l
ocal/share/:/usr/share/DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbusMKUBWsJj9t,guid=506ca60e0758d238806a3de600000015LESSOPEN=| /usr/bin/lesspipe
%sDISPLAY=:0XDG_CURRENT_DESKTOP=UnityLESSCLOSE=/usr/bin/lesspipe %s
%sCOLORTERM=gnometerminalXAUTHORITY=/home/aits/.XauthorityOLDPWD=/home/aits_=./a.out
****************************************************************************/

/************************************************************************
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'
************************************************************/

You might also like