Thursday, February 4, 2010

BASIC UNIX COMMANDS

BASIC NETWORK COMMANDS


[user1@localhost user1]$ cat>file
we can create a file

[user1@localhost user1]$ ls -l
-rw-r--r-- 1 user1 26jpmc 37 Dec 2 19:11 file
drwxr-xr-x 2 user1 26jpmc 4096 Dec 2 14:09 anbu

[user1@localhost user1]$ chmod 777 file

[user1@localhost user1]$ ls -l
-rwxrwxrwx 1 user1 26jpmc 37 Dec 2 19:11 file
drwxr-xr-x 2 user1 26jpmc 4096 Dec 2 14:09 anbu

[user1@localhost user1]$ rm file

[user1@localhost user1]$ pwd
/home/26jpmc/user1

[user1@localhost user1]$ mkdir new

user1@localhost user1]$ ls
new anbu

[user1@localhost user1]$ rmdir new

[user1@localhost user1]$ cd anbu

[user1@localhost anbu]$ ls
fact fon ooe sal

[user1@localhost anbu]$ cp sal salary

[user1@localhost anbu]$ mv ooe odd

[user1@localhost anbu]$ logname
User2

[user1@localhost anbu]$ id
uid=541(user1) gid=501(26jpmc) groups=501(26jpmc)

[user1@localhost anbu]$ uname
Linux
[user1@localhost anbu]$ tty
/dev/pts/6

[user1@localhost anbu]$ who
User3 pts/5 Dec 1 18:48 (170.100.40.46)
User4 pts/14 Dec 1 18:49 (170.100.40.1)
user5 pts/7 Dec 1 18:51 (170.100.40.19)
user6 pts/4 Dec 1 19:17 (170.100.40.3)

[user1@localhost anbu]$ who am i
User7 pts/6 Dec 1 20:09 (170.100.40.45)

[user1@localhost anbu]$ cal
December 1999
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31



Factorial number:

echo enter the number
read n
i=1
f=1
while [ $i -le $n ]
do
f=` expr $i \* $f `
i=` expr $i \+ 1 `
done
echo result is $f

[user1@localhost anbu]$ sh fact
enter the number
4
result is 24

Odd or Even:

echo enter the number
read a
f=` expr $a % 2 `
if test $f -eq 1
then
echo $a is ood
else
echo $a is even
fi

[user1@localhost anbu]$ sh odd
enter the name
11
11 is odd
Basic Salary:
echo enter the basic salary
read bs
if [ $bs -lt 1500 ]
then
hra=` echo $bs \* 10 / 100 bc `
da=` echo $bs \* 90 / 100 bc `
else
hra=500
dra=` echo $bs \* 98 / 100 bc `
fi
gs=` echo $bs + $hra + $da bc `
echo gs=Rs. $gs

[user1@localhost anbu]$ sh sal
enter the basic salary
1200
gs=Rs. 2400

File or Not:

echo enter the file name
read fname
if [ -f $fname ]
then
echo filename
else
echo not filename
fi

[user1@localhost anbu]$ sh fon
enter the file name
salary
filename

PARENT AND CHILD PROCESS ID

PARENT AND CHILD PROCESS ID PROGRAM

#include “ sys/types.h “
#include “ sys/stat.h “
main()
{
int p1,p2,p3,p4,p5;
p1=getpid();
system("clear");
printf("\n process 1=%d\n",p1);
p2=fork();
if(p2==0)
{
p1=wait();
printf("parent process 2=%d\n",getpid());
printf("\n process 2=%d\n",getpid());
p3=fork();
p1=wait();
if(p3==0)
{
printf("\n parent process 3=%d\n",getpid());
printf("\nprocess 3=%d\n",getpid());
p4=fork();
if(p4==0)
{
p3=wait();
printf("\n parent process 4=%d\n",getppid());
printf("\n process 4=%d\n",getpid());
}
if(p4 “ 0)
{
p4=wait();
p3=wait();
p5=fork();
if(p5==0)
{
printf("\nparent process 5=%d\n",getppid());
printf("\nprocess 5=%d\n",getpid());
}
}
}
}
}

MESSAGE QUEUE

MESSAGE QUEUE


#include “ stdio.h “
#include “ stdlib.h “
#include “ sys/types.h “
#include “ sys/ipc.h “
#include “ unistd.h “
#define msgtxtlen 100
#define sizeofmsg sizeof(msg)
#define msglen strlen(msg.mtext)
struct msgbuf
{
long mtype;
char mtext[msgtxtlen];
}msg;
main()
{
int ch;
int msgqid,cont=1,ren;
while(cont)
{
printf("\n\t Message Queue Manipulation ");
printf("\n1.Create message queue");
printf("\n2.Add a message into the queue");
printf("\n3.Retrive a message from the queue");
printf("\n4.Remove a message queue");
printf("\n5.Exit");
printf("\nEnter the choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
msgqid=msgget(0x25,IPC_CREAT0644);
printf("\n\tMessage Queue is created");
break;
case 2:
printf("\n\tMessage Sending");
printf("\n\t------- -------");
printf("\n\tEnter the message type");
scanf("%d",&msg.mtype);
printf("\nEnter the message ");
scanf("%s",&msg.mtext);
ren=msgsnd(msgqid,&msg,sizeofmsg,0);


if(ren!=1)
printf("\nMessage posted successfully");
else
printf("\nSorry you made a mistake");
break;
case 3:
printf("\nMessage receving & removing from the queue");
printf("\nEnter the message type");
scanf("%d",&msg.mtype);
ren=msgrcv(msgqid,&msg,sizeofmsg,msg.mtype,0);
printf("\nMessage type %d",msg.mtype);
printf("\nMessage length %d",msglen);
printf("\nMessage content %s",msg.mtext);
break;
case 4:
ren=msgctl(msgqid,IPC_RMID,NULL);
if(ren!=1)
printf("\n\tMessage queue was removed");
else
printf("\n\tSorry");
break;
case 5:
exit(0);
default:
printf("\n\t Enter the value choice");
break;
}
printf("\nDo you want to contine y=1/n=0 : ");
scanf("%d",&cont);
}
}

MESSAGE QUEUE

MESSAGE QUEUE


#include “ stdio.h “
#include “ stdlib.h “
#include “ sys/types.h “
#include “ sys/ipc.h “
#include “ unistd.h “
#define msgtxtlen 100
#define sizeofmsg sizeof(msg)
#define msglen strlen(msg.mtext)
struct msgbuf
{
long mtype;
char mtext[msgtxtlen];
}msg;
main()
{
int ch;
int msgqid,cont=1,ren;
while(cont)
{
printf("\n\t Message Queue Manipulation ");
printf("\n1.Create message queue");
printf("\n2.Add a message into the queue");
printf("\n3.Retrive a message from the queue");
printf("\n4.Remove a message queue");
printf("\n5.Exit");
printf("\nEnter the choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
msgqid=msgget(0x25,IPC_CREAT0644);
printf("\n\tMessage Queue is created");
break;
case 2:
printf("\n\tMessage Sending");
printf("\n\t------- -------");
printf("\n\tEnter the message type");
scanf("%d",&msg.mtype);
printf("\nEnter the message ");
scanf("%s",&msg.mtext);
ren=msgsnd(msgqid,&msg,sizeofmsg,0);


if(ren!=1)
printf("\nMessage posted successfully");
else
printf("\nSorry you made a mistake");
break;
case 3:
printf("\nMessage receving & removing from the queue");
printf("\nEnter the message type");
scanf("%d",&msg.mtype);
ren=msgrcv(msgqid,&msg,sizeofmsg,msg.mtype,0);
printf("\nMessage type %d",msg.mtype);
printf("\nMessage length %d",msglen);
printf("\nMessage content %s",msg.mtext);
break;
case 4:
ren=msgctl(msgqid,IPC_RMID,NULL);
if(ren!=1)
printf("\n\tMessage queue was removed");
else
printf("\n\tSorry");
break;
case 5:
exit(0);
default:
printf("\n\t Enter the value choice");
break;
}
printf("\nDo you want to contine y=1/n=0 : ");
scanf("%d",&cont);
}
}

PIPES Program

PIPES

#include "fcntl.h"
#include "errno.h"
#include "sys/ipc.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "sys/stat.h"
#include "sys/types.h"
#include "unistd.h"
#define MAX 100
void client(int,int);
void server(int,int);
int main(void)
{
int p1[2],p2[2];
pid_t cid;
pipe(p1);
pipe(p2);
if((cid=fork())==0)
{
close(p1[1]);
close(p2[0]);
server(p1[0],p2[1]);
exit(0);
}
close(p1[0]);
close(p2[1]);
client(p2[0],p1[1]);
waitpid(cid,NULL,0);
exit(0);
}

void client(int f1,int f2)
{
size_t len,n;
char buf[MAX];
printf("\n Entetr the filename.");
fgets(buf,MAX,stdin);
len=strlen(buf);
if(buf[len-1]=='\n')
len--;

write(f2,buf,len);
while(n=read(f1,buf,MAX))
write(STDOUT_FILENO,buf,n);
}

void server(int f1,int f2)
{
int fd;
size_t n;
char buf[MAX+1];
if((n=read(f1,buf,MAX))==0)
printf("End of the file");
buf[n]='\0';
if((fd=open(buf,O_RDONLY)) “ 0)
{
snprintf(buf+n,sizeof(buf)-n,":cannot open %s\n",strerror(errno));
n=strlen(buf);
write(f2,buf,n);
}
else
{
while((n=read(fd,buf,MAX)) “ 0)
write(f2,buf,n);
printf("\n%s",buf);
close (fd);
}

}

IMPLEMENTATION OF SYSTEM CALLS

IMPLEMENTATION OF SYSTEM CALLS

#include “ stdio.h “
#include “ sys/types.h “
#include “ sys/dir.h “
#include “ sys/stat.h “
#include “ fcntl.h “
#include “ unistd.h “
#define size 512
char buff[size],name[20];
int fd,n,k;
main()
{
int ch,nread,nwrite,a,pos,i,j;
long int of;
struct stat buf;
do
{
printf("\n\n\t IMPLEMENTATION OF SYSTEM CALLS");
printf("\n\t...................................");
printf("\n\t1.Create a file");
printf("\n\t2.Open a file");
printf("\n\t3.Write into the file");
printf("\n\t4.Read the file contents");
printf("\n\t5.Seek the file");
printf("\n\t6.View the status of the file");
printf("\n\t7.Close the file");
printf("\n\t8.exit");
printf("\n\tEnter Ur Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\tEnter name of file:");
scanf("%s",name);
fd=creat(name,S_IRUSRS_IWUSRS_IXUSR);
if(fd==1)
printf("\n\tError in creating file");
else
{
printf("\n\tFile created");
printf("\n\tThe descriptor is %d",fd);
}
break;
case 2:
printf("\n\t Enter the name of the file:");


scanf("%s",name);
fd=open(name,2);
if(fd==1)
printf("\n\tError in opening the file");
else
printf("\n\tFile opened with fd %d",fd);
break;
case 3:
printf("\n\tEnter the info to be stored in the file");
scanf("%s",buff);
if(write(fd,buff,sizeof(buff)) “ 0)
printf("\n\tError in writing into the file");
else
printf("\n\tWrite Successful");
break;
case 4:
printf("\n\tThe contents of the file:\n");
nread=read(fd,buff,sizeof(buff));
if(nread!= -1)
printf("\n\t%s",buff);
break;
case 5:
printf("\n\tEnter the offset:");
scanf("%d",&of);
printf("\n\tEnter the position:");
scanf("%d",&pos);
if(a== -1)
printf("\n\tSeek Error");
else
printf("\n\tSeek Ok");
break;
case 6:
i=stat(name,&buf);
j=fstat(fd,&buf);
if(i==0 && j==0)
printf("\n\tSTAT & FSTAT successful");
else
printf("\n\tstat &fstat error");
printf("\n\tFile %s has %d links:",name,buf.st_nlink);
printf("\n\tFile inode number:%d",buf.st_ino);
printf("\n\tUserid:%d",buf.st_uid);
break;
case 7:
k=close(fd);


if(k==1)
printf("\n\tError in closing the file");
else
printf("\n\tFile Closed Successfully");
break;
case 8:
printf("\n\n\t\tPress any key.");
break;

}
} while(ch!=8);
}

UDP SOCKETS

UDP SOCKETS



#include “ netinet/in.h “
main()
{
char buff[512]={" "};
int id,b,r,n;
struct sockaddr_in server,client;
id=socket(AF_INET,SOCK_DGRAM,0);
if(id “ 0)
{
write(1,"connection error",17);
exit(0);
}
server.sin_family=AF_INET;
server.sin_port=8510;
server.sin_addr.s_addr=inet_addr("170.100.40.179");
b=bind(id,(struct sockaddr *)&server,sizeof(server));
if(b “ 0)
{
write(1,"bind error",10);
exit(0);
}
n=sizeof(client);
r=recvfrom(id,(void *)&buff,sizeof(buff),0,(struct sockaddr *)&client,&n);
if(r “ 0)
{
write(1,"receive error",13);
exit(0);
}
printf("\nData received");
write(1,buff,sizeof(buff));
}

TCP SOCKET Server Program

TCP SERVER :

#include “ sys/socket.h “
#include “ unistd.h “
#include “ fcntl.h “
#include “ netinet/in.h “
main()
{
char buff[512]={""},buf[512]={""};
int id,a,b,l,n,r,s;
struct sockaddr_in server,client;
id=socket(AF_INET,SOCK_STREAM,0);
if(id “ 0)
{
write(1,"connection error",17);
exit(0);
}
server.sin_family=AF_INET;
server.sin_port=htons(1025);
server.sin_addr.s_addr=inet_addr("170.100.40.180");
client.sin_family=AF_INET;
client.sin_port=htons(1025);
client.sin_addr.s_addr=inet_addr("170.100.40.180");
b=bind(id,(struct sockaddr *) &server,sizeof(server));
if(b “ 0)
{
write(1,"Bind Error\n",10);
exit(0);
}
l=listen(id,1);
n=sizeof(client);
a=accept(id,(struct sockaddr *) &client,&n);
if(a “ 0)
{
write(1,"Accept error\n",12);
exit(0);
}






r=read(a,buff,sizeof(buff));
if(r “ 0)
{
write(1,"Recieve Error\n",13);
exit(0);
}
write(1,buff,sizeof(buff));
write(a,"Recieved\n",8);
printf("\nPort Number : %u",client.sin_port);
printf("\nIP Address : %s",inet_ntoa(client.sin_addr));
close(id);
}

TCP SOCKETS Client Program

TCP SOCKETS

TCP CLIENT :

#include “ sys/socket.h “
#include “ unistd.h “
#include “ fcntl.h “
#include “ netinet/in.h “
#include “ netdb.h “
main()
{
char buff[512]={""},buf[512]={""};
int id,a,b,c,l,n,r,s;
struct sockaddr_in server,client;
id=socket(AF_INET,SOCK_STREAM,0);
if(id “ 0)
{
write(1,"connection error",17);
exit(0);
}
server.sin_family=AF_INET;
server.sin_port=htons(1025);
server.sin_addr.s_addr=inet_addr("170.100.40.180");
client.sin_family=AF_INET;
client.sin_port=htons(1025);
client.sin_addr.s_addr=inet_addr("170.100.40.180");
b=bind(id,(struct sockaddr *) &client,sizeof(client));
if(b “ 0)
{
write(1,"Bind Error\n",11);
exit(0);
}
c=connect(id,(struct sockaddr *) &server,sizeof(server));
if(c “ 0)
{
write(1,"Connect error\n",11);
exit(0);
}




write(1,"Enter the data : ",18);
read(0,buff,sizeof(buff));
if(s “ 0)
{
write(1,"Send Error\n",11);
exit(0);
}
n=read(id,buff,sizeof(buff));
write(1,buff,sizeof(buff));
close(id);
}

SYSTEM CALLS

SYSTEM CALLS

#include “ stdio.h “
#include “ sys/types.h “
#include “ sys/dir.h “
#include “ sys/stat.h “
#include “ fcntl.h “
#include “ unistd.h “
#define size 512
char buff[size],name[20];
int fd,n,k;
main()
{
int ch,nread,nwrite,a,pos,i,j;
long int of;
struct stat buf;
do
{
printf("\n\n\t IMPLEMENTATION OF SYSTEM CALLS");
printf("\n\t1.Create a file");
printf("\n\t2.Open a file");
printf("\n\t3.Write into the file");
printf("\n\t4.Read the file contents");
printf("\n\t5.Seek the file");
printf("\n\t6.View the status of the file");
printf("\n\t7.Close the file");
printf("\n\t8.exit");
printf("\n\tEnter Ur Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\tEnter name of file:");
scanf("%s",name);
fd=creat(name,S_IRUSRS_IWUSRS_IXUSR);
if(fd==1)
printf("\n\tError in creating file");
else
{
printf("\n\tFile created");
printf("\n\tThe descriptor is %d",fd);
}
break;

case 2:
printf("\n\t Enter the name of the file:");
scanf("%s",name);
fd=open(name,2);
if(fd==1)
printf("\n\tError in opening the file");
else
printf("\n\tFile opened with fd %d",fd);
break;

case 3:
printf("\n\tEnter the info to be stored in the file");
scanf("%s",buff);
if(write(fd,buff,sizeof(buff)) “ 0)
printf("\n\tError in writing into the file");
else
printf("\n\tWrite Successful");
break;

case 4:
printf("\n\tThe contents of the file:\n");
nread=read(fd,buff,sizeof(buff));
if(nread!= -1)
printf("\n\t%s",buff);
break;

case 5:
printf("\n\tEnter the offset:");
scanf("%d",&of);
printf("\n\tEnter the position:");
scanf("%d",&pos);
if(a== -1)
printf("\n\tSeek Error");
else
printf("\n\tSeek Ok");
break;

case 6:
i=stat(name,&buf);
j=fstat(fd,&buf);
if(i==0 && j==0)
printf("\n\tSTAT & FSTAT successful");
else
printf("\n\tstat &fstat error");
printf("\n\tFile %s has %d links:",name,buf.st_nlink);
printf("\n\tFile inode number:%d",buf.st_ino);
printf("\n\tUserid:%d",buf.st_uid);
break;

case 7:
k=close(fd);
if(k==1)
printf("\n\tError in closing the file");
else
printf("\n\tFile Closed Successfully");
break;

case 8:
printf("\n\n\t\tPress any key.");
break;

}
}while(ch!=8);
}


















OUTPUT

[user1@localhost user1]$ cc syscall.c
[user1@localhost user1]$ ./a.out


IMPLEMENTATION OF SYSTEM CALLS
1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:1

Enter name of file:unix

File created
The descriptor is 3

IMPLEMENTATION OF SYSTEM CALLS
1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:2

Enter the name of the file:unix

File opened with fd 4

IMPLEMENTATION OF SYSTEM CALLS

1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:3

Enter the info to be stored in the file
operatingsystem

Write Successful

IMPLEMENTATION OF SYSTEM CALLS
1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:4

The contents of the file:

operatingsystem

IMPLEMENTATION OF SYSTEM CALLS
1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:5

Enter the offset:0

Enter the position:1

Seek Ok

IMPLEMENTATION OF SYSTEM CALLS
1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:6

STAT & FSTAT successful
File unix has 1 links:
File inode number:4383000
Userid:515

IMPLEMENTATION OF SYSTEM CALLS
...................................
1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:7

File Closed Successfully

IMPLEMENTATION OF SYSTEM CALLS
...................................
1.Create a file
2.Open a file
3.Write into the file
4.Read the file contents
5.Seek the file
6.View the status of the file
7.Close the file
8.exit
Enter Ur Choice:8


Press any key.[user1@localhost user1]$

SHARED MEMORY

INTERPROCESS COMMUNICATION USING SHARED MEMORY

#include “ stdio.h “
#include “ stdlib.h “
#include “ string.h “
#include “ sys/types.h “
#include “ sys/ipc.h “
#include “ sys/shm.h “
#define SHMSIZE 1024

int main()
{
key_t key;
int shmid,ch=0;
struct shmid_ds s;
char d[10];
char *data;
int mode;

while(ch “ =7)
{
printf("\n1.Create");
printf("\n2.Attach");
printf("\n3.Operation");
printf("\n4.Retrieve");
printf("\n5.Detach");
printf("\n6.Check Struct value after attaching");
printf("\n7.Exit");
printf("\nEnter Ur Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if((key=ftok("hema.c",'R'))==-1)
{
perror("ftok");
exit(1);
}
if((shmid=shmget(key,SHMSIZE,10644IPC_CREAT))==1)
{
perror("shmget");
exit(1);
}
break;

case 2:
data=shmat(shmid,NULL,0);
if(data==(char *)(-1))
{
perror("shmat");
exit(1);
}
break;

case 3:
printf("\nWriting to segment");
printf("\nEnter the data:");
scanf("%s",d);
strcpy(data,d);
break;

case 4:
printf("\nSegment contains %s",data);
break;

case 5:
if(shmdt(data)==-1)
{
perror("shmdt");
exit(1);
}
break;

case 6:
shmctl(shmid,IPC_STAT,&s);
printf("\nProcess Id :%d",getpid());
printf("\tTime=%time \t attach %d",s.shm_atime,s.shm_nattch);
system("ipcs_mhead");
break;

case 7:
exit(0);
}
}
return 0;
}





OUTPUT

[user1@localhost user1]$ cc shared.c
[user1@localhost user1]$ ./a.out

1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit

Enter Ur Choice:1

1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit

Enter Ur Choice:2

1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit

Enter Ur Choice:3
Writing to segment
Enter the data:welcome

1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit

Enter Ur Choice:4

Segment contains welcome
1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit

Enter Ur Choice:5

1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit

Enter Ur Choice:6

sh: line 1: ipcs_m: command not found
Process Id :4249 Time=1228356735me attach 0

1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit

Enter Ur Choice:7

[user1@localhost user1]$

SHARED MEMORY

INTERPROCESS COMMUNICATION USING SHARED MEMORY

#include “ stdio.h “
#include “ stdlib.h “
#include “ string.h “
#include “ sys/types.h “
#include “ sys/ipc.h “
#include “ sys/shm.h “
#define SHMSIZE 1024

int main()
{
key_t key;
int shmid,ch=0;
struct shmid_ds s;
char d[10];
char *data;
int mode;

while(ch “ =7)
{
printf("\n1.Create");
printf("\n2.Attach");
printf("\n3.Operation");
printf("\n4.Retrieve");
printf("\n5.Detach");
printf("\n6.Check Struct value after attaching");
printf("\n7.Exit");
printf("\nEnter Ur Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if((key=ftok("hema.c",'R'))==-1)
{
perror("ftok");
exit(1);
}
if((shmid=shmget(key,SHMSIZE,10644IPC_CREAT))==1)
{
perror("shmget");
exit(1);
}
break;

case 2:
data=shmat(shmid,NULL,0);
if(data==(char *)(-1))
{
perror("shmat");
exit(1);
}
break;

case 3:
printf("\nWriting to segment");
printf("\nEnter the data:");
scanf("%s",d);
strcpy(data,d);
break;

case 4:
printf("\nSegment contains %s",data);
break;

case 5:
if(shmdt(data)==-1)
{
perror("shmdt");
exit(1);
}
break;

case 6:
shmctl(shmid,IPC_STAT,&s);
printf("\nProcess Id :%d",getpid());
printf("\tTime=%time \t attach %d",s.shm_atime,s.shm_nattch);
system("ipcs_mhead");
break;

case 7:
exit(0);
}
}
return 0;
}





OUTPUT

[user1@localhost user1]$ cc shared.c
[user1@localhost user1]$ ./a.out

1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit

Enter Ur Choice:1

1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit

Enter Ur Choice:2

1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit

Enter Ur Choice:3
Writing to segment
Enter the data:welcome

1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit

Enter Ur Choice:4

Segment contains welcome
1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit

Enter Ur Choice:5

1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit

Enter Ur Choice:6

sh: line 1: ipcs_m: command not found
Process Id :4249 Time=1228356735me attach 0

1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit

Enter Ur Choice:7

[user1@localhost user1]$

SEMAPHORES

INTERPROCESS COMMUNICATION USING SEMAPHORES

#include “ sys/sem.h “
#include “ sys/types.h “
#include “ fcntl.h “
int main()
{
int id,a;
struct sembuf s;
id=semget(IPC_PRIVATE,1,10644IPC_CREAT);
a=fork();
if(a==0)
{
sleep(1);
printf("\nChild before semaphore operation\n");
s.sem_num=0;
s.sem_op=0;
semop(id,&s,1);
printf("\nChild ok\n");
}
else
{
printf("\nParent before semctl\n");
semctl(id,0,SETVAL,1);
printf("\nParent sleeping\n");
sleep(3);
printf("\nParent before 2 semctl\n");
semctl(id,0,SETVAL,0);
printf("\nParent ok\n");
exit(0);
}
}

OUTPUT
[user1@localhost user1]$ cc semaphore.c
[user1@localhost user1]$ ./a.out

Parent before semctl

Parent sleeping

Child before semaphore operation

Child ok

Parent before 2 semctl

Parent ok
[user1@localhost user1]$