main()
{
int childpid;
if(childpid=fork()<0) exit(0); if(childpid>0)
{
printf("***Parent Process***\n");
printf("My pid=%d\nMy parent pid=%d\n",getpid(),getppid());
while(wait((int *)0)!=childpid);
}
else
{
printf("***Child process***\n");
printf("My pid=%d\nMy parentpid=%d\n",getpid(),getppid());
}
}
Output:
[mtechcs@cse11 007]$ ./a.out
***Child process***
My pid=6543
My parentpid=6542
***Parent process***
My pid=6542
My parentpid=6507
[mtechcs@cse11 007]$
//Pipes (pipes.c)
#define MAX 100
main()
{
int childpid,pipe1[2],pipe2[2],nread,nwrite;
char buff[MAX];
//pipe(pipe1);
//pipe(pipe2);
if((pipe(pipe1)<0||pipe(pipe2)<0)) { printf("ERROR: pipes not created"); exit(0); } if((childpid=fork())<0) { printf("error in process\n"); exit(0); } if(childpid>0)
{
printf("writing to the server\n");
close(pipe1[0]);
close(pipe2[1]);
nread=read(0,buff,MAX);
nwrite=write(pipe1[1],buff,nread);
if(nread!=nwrite)
printf("Write Error");
nread=read(pipe2[0],buff,MAX);
nwrite=write(1,buff,nread);
if(nread!=nwrite)
printf("Standard Output Error\n");
while(wait((int*)0)!=childpid);
close(pipe1[1]);
close(pipe2[0]);
exit(0);
}
else
{
close(pipe1[1]);
close(pipe2[0]);
nread=read(pipe1[0],buff,MAX);
strcat(buff,"from the server");
nread=strlen(buff);
nwrite=write(pipe2[1],buff,nread);
close(pipe1[0]);
close(pipe2[1]);
exit(0);
}
}
Output:
[mtechcs@cse11 007]$ cc pipes.c
[mtechcs@cse11 007]$ ./a.out
Writing to the server
Pipes created
Pipes created
from the server[mtechcs@cse11 007]$
//Related FIFO (relfifo.c)
#include
#define MAX 100
#define FIFO1 "./FIFO1"
#define FIFO2 "./FIFO2"
#define PERMS 0666
main()
{
int childpid,readfd,writefd,nread,nwrite;
char buff[MAX];
if(mknod(FIFO1,S_IFIFO|PERMS,0)<0) printf("FIFO1 created\n"); if(mknod(FIFO2,S_IFIFO|PERMS,0)<0) printf("FIFO2 created\n"); else unlink(FIFO1); if((childpid=fork())<0) { printf("error in process\n"); exit(0); } if(childpid>0)
{
printf("***Parent Process***\nWriting to the server\n");
writefd=open(FIFO1,1);
readfd=open(FIFO2,0);
nread=read(0,buff,MAX);
nwrite=write(writefd,buff,nread);
if(nread!=nwrite)
printf("Write Error!\n");
nread=read(readfd,buff,MAX);
nwrite=write(1,buff,nread);
if(nread!=nwrite)
printf("Standard Output Error\n");
while(wait((int*)0)!=childpid);
close(readfd);
close(writefd);
unlink(FIFO1);
unlink(FIFO2);
exit(0);
}
else
{
readfd=open(FIFO1,0);
writefd=open(FIFO2,1);
nread=read(readfd,buff,MAX);
strcat(buff,"from the server");
nread=strlen(buff);
nwrite=write(writefd,buff,nread);
close(readfd);
close(writefd);
exit(0);
}
}
Output:
[mtechcs@cse11 007]$ cc relfifo.c
[mtechcs@cse11 007]$ ./a.out
FIFO1 created
FIFO2 created
***Parent process***
Writing to the server
M.Tech IT
M.Tech IT
from the server Write error!
[mtechcs@cse11 007]$
// Unrelated FIFO
//Server (servfifo.c)
#include"myfifo.h"
main()
{
int childpid,readfd,writefd,nread,nwrite;
char buff[MAX];
if(mknod(FIFO1,S_IFIFO|PERMS,0)<0) printf("FIFO1 created\n"); if(mknod(FIFO2,S_IFIFO|PERMS,0)<0) printf("FIFO2 created\n"); else unlink(FIFO1); if((childpid=fork())<0) { printf("error in process\n"); exit(0); } if(childpid>0)
{
printf("***Parent Process***\nWriting to the server\n");
readfd=open(FIFO1,1);
writefd=open(FIFO2,0);
nread=read(readfd,buff,MAX);
nwrite=write(writefd,buff,nread);
if(nread!=nwrite)
printf("Write Error\n");
nread=read(readfd,buff,MAX);
nwrite=write(1,buff,nread);
if(nread!=nwrite)
printf("Standard Output Error\n");
while(wait((int*)0)!=childpid);
exit(0);
}
}
//Client (clififo.c)
#include"myfifo.h"
jcjmain()
{
int childpid,readfd,writefd,nread,nwrite;
char buff[MAX];
printf("***Child Process***\n");
writefd=open(FIFO1,1);
readfd=open(FIFO2,0);
nread=read(0,buff,MAX);
strcat(buff,"from the server");
nread=strlen(buff);
nwrite=write(writefd,buff,nread);
close(readfd);
close(writefd);
unlink(FIFO1);
unlink(FIFO2);
exit(0);
}
//Header file (myfifo.h)
#include
#define MAX 100
#define PERMS 0666
#define FIFO1 "/user/tmp/FIFO1"
#define FIFO2 "/user/tmp/FIFO2"
Output:
[mtechcs@cse11 007]$cc –o client clififo.c
[mtechcs@cse11 007]$cc –o server servfifo.c
[mtechcs@cse11 007]$./server&
[mtechcs@cse11 007]$./client
FIFO1 created
FIFO2 created
***Parent Process***
Writing to the server
hello
***Client Process***
hello
from the server
//Message Queues
//Header File (mymsg.h)
#include
#include
#include
#include
#define PERMS 0666
#define MHDRSIZE sizeof(mesg)-MAXDATASIZE;
#define MAXDATASIZE 1024
#define MKEY1 1234L
#define MKEY2 2345L
typedef struct msgbuff{
int mesg_len;
long int mesg_type;
char mesg_data[MAXDATASIZE];
}mesg;
//Client (climsg.c)
#include"mymsg.h"
main()
{
int readqid,writeqid;
writeqid=msgget(MKEY1,0);
readqid=msgget(MKEY2,0);
client(readqid,writeqid);
msgctl(readqid,IPC_RMID,0);
msgctl(writeqid,IPC_RMID,0);
exit(0);
}
client(readqid,writeqid)
{
int len,nread;
mesg message;
nread=read(0,message.mesg_data,MAXDATASIZE);
message.mesg_type=1;
msgsnd(writeqid,(struct type *)message.msgbuff,nread,0);
msgrcv(readqid,message.mesg_data,message.mesg_len,0,0);
write(1,message.mesg_data,MAXDATASIZE);
}
//Server (servmsg.c)
#include"mymsg.h"
mesg message;
int nread;
main()
{
int readqid,writeqid;
readqid=msgget(MKEY1,PERMS|IPC_CREAT);
writeqid=msgget(MKEY2,PERMS|IPC_CREAT);
server(readqid,writeqid);
exit(0);
}
server(readqid,writeqid)
{
msgrcv(readqid,message.mesg_data,MAXDATASIZE,message.mesg_type,0);
msgsnd(writeqid,(struct type *)msgbuff,nread,0);
}
//Shared memory
//Header File (myshm.h)
#include
#include
#include
#define PERMS 0666
#define MHDRSIZE sizeof(mesg)-MAXDATASIZE;
#define MAXDATASIZE 100
#define SHMKEY 1234L
typedef struct msgbuff{
int mesg_len;
long int mesg_type;
char mesg_data[MAXDATASIZE];
}mesg;
//Server (servshm.c)
#include"myshm.h"
mesg *msgptr;
main()
{
int shmid;
shmid=shmget(SHMKEY,sizeof(mesg),PERMS|IPC_CREAT);
msgptr=(mesg *)shmat(shmid,0,0);
strcat(msgptr->mesg_data,"FROM SERVER");
mshptr->mesg_type=2L;
msgptr->mesg_len=strlen(msgptr->mesg_data);
}
//Client (clishm.c)
#include"myshm.h"
Mesg *msgptr;
main()
{
int shmid;
shmid=shmget(SHMKEY,sizeof(mesg),0);
msgptr=(mesg *)shmat(shmid,0,0);
nread=read(0,msgptr->mesg_data,MAXDATASIZE);
msgptr->mesg_len=nread;
msgptr->mesg_type=1L;
write(1,msgptr->mesg_data,msgptr->mesg_len);
shmdt(msgptr);
shmctl(shmid,IPC_RMID,0);
}
//Semaphores
//Header File (mysem.h)
#include
#include
#include
#define PERMS 0666
#define MHDRSIZE sizeof(mesg)-MAXDATASIZE;
#define MAXDATASIZE 1024
#define SHMKEY 4567L
#define SEMKEY1 1234L
#define SEMKEY2 2345L
typedef struct msgbuff{
int mesg_len;
long int mesg_type;
char mesg_data[MAXDATASIZE];
}mesg;
int shmid,semid;
typedef struct sembuff{
short sem_num;
short sem_op;
short sem_flg;
}semp;
static struct sembuff myop_lock[2]= {
0,0,0;
0,1,SEM_UNDO;
};
static struct sembuff myop_unlock[1]={
0,-1,SEM_UNDO;
};
Static struct sembuff op_endcrete[2]={
1,-1,SEM_UNDO;
2,-1,SEM_UNDO;
};
static struct sembuff op_lock[2]= {
0,0,0;
0,1,SEM_UNDO;
};
static struct sembuff op_open[1]= {
1,-1,SEM_UNDO;
};
static struct sembuff op_close[3]={
2, 0, 0,
2, 1, SEM_UNDO,
1, 1, SEM_UNDO
};
Static stuct sembuff op_unlock[1]={
2,-1,SEM_UNDO;
};
//sem_create
int sem_create(int key,int initval)
{
int semid,semval;
semid=semget(key,3,PERMS|IPC_CREAT);
if(initval==0)
semop(semid,&op_lock[0],2);
if((semval=semctl(semid,1,GETVAL,0))==0)
semctl(semid,1,SETVAL,BIGCOUNT)
semctl(semid,0,SETVAL,initval);
semop(semid,&op_endcreate[0],2);
return(semid);
}
//sem_open
int sem_open(int key)
{
int semid;
semid=semget(key,3,0);
if(semid<0) printf(“UNSUCCESSFULL”); semop(id,&op_open[0],1); return(semid); } //sem_close sem_close(int semid) { int semval; semop(id,&op_close[0],3); if((semval=semctl(id,1,GETVAL,0))==BIGCOUNT) sem_rm(semid); else semop(id,&op_unlock[0],1); return(semid); } //sem_wait sem_wait(int semid,value) { op_op[0].val=value semop(semid,&op_op[0],1); } //sem_signal sem_signal(int semid) { op_op[0].val=value; semop(semid,&op_ops[0],1); } //sem_rm Sem_rm(semid) { Semctl(semid,IPC_RMID,0); } //Server #include"mysem.h" main() { mesg msgptr; shmid=shmget(SHMKEY,sizeof(mesg),PERMS|IPC_CREAT); msgptr=(mesg *)shmat(shmid,0,0); clisem=sem_create(SEMKEY2,0); servsem=sem_create(SEMKEY1,1); sem_wait(clisem); server(); shmdt(msgptr); } server() { strcat(msgptr->mesg_data,"from server");
msgptr->mesg_len=strlen();
msgptr->mesg_type=2L;
sem_signal(servsem);
}
//Client
#include"mysem.h"
main()
{
Mesg msgptr;
shmid=shmget(SHMKEY,0,0);
msgptr=(Mesg *)shmat(shmid,0,0);
sem_wait(servsem);
client();
shmdt(msgptr);
shmctl(shmid,IPC_RMID,0);
sem_rm(clisem);
sem_rm(servsem);
client()
{
read(0,msgptr->mesg_data,MAXDATASIZE);
sem_signal(clisem);
sem_wait(servsem);
write(1,msgptr->mesg_data,msgptr->mesg_len);
}
//TCP client server Application
//tcp_echo header (tcp_header.h)
#include
#include
#define SERV_PORT 9999
#define MAXSIZE 1024
#define SERV_ADDR "172.16.5.80"
//readn function
readn(fd,buff,nbytes)
{
int fd,nbytes,nleft,nread;
pbuff=buff;
nleft=nbytes
while(nleft>0)
{
nread=read(fd,pbuff,nleft);
if(nread==0)
break;
if(nread<0) nleft=nleft-nread; buff=pbuff+nread; } return(nbytes-nleft); } //writen function writen(fd,buff,nbytes) { int fd,nbytes,nleft,nwrite; pbuff=buff; nleft=nbytes; while(nleft>0)
{
nwrite=write(fd,pbuff,nleft)
if(nwrite<0) nleft=nleft-nwrite; buff=pbuff+nwrite; } return(nbytes-nleft); } //tcp echo server
#include”tcp_header.h”
main()
{
int listenfd,connfd,clilen,n;
struct sockaddr_in servaddr,cliaddr;
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDRANY);
servaddr.sin_port=htons(SERV_PORT);
bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
listen(listenfd,6);
for(;;)
{
clilen=sizeof(cliaddr);
connfd=accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);
n=readn(connfd,buff,MAXBUFF);
writen(connfd,buff,n);
exit(0);
close(connfd);
}
}
//tcp_echo client
#include”tcp_header.h”
main()
{
int sockfd,n;
struct sockaddr_in servaddr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERV_PORT);
servaddr.sin_addr.s_addr=htonl(inet_addr(SERV_ADDR);
connect(sockfd,(struct sockaddr *)servaddr,sizeof(servaddr));
n=read(0,buff,BUFFSIZE);
writen(sockfd,buff,n);
n=readn(sockfd,buff,BUFFSIZE);
write(1,buff,n);
close(sockfd);
}
//UDP client server Application
//udp echo server
#include
#include
#include
#define SERV_PORT 9999
#define MAXSIZE 1024
main()
{
int nbytes,sockfd;
char buff[MAXSIZE];
struct sockaddr_in servaddr,cliaddr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
nbytes=read(0,buff,MAXSIZE);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERV_PORT);
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
bind(sockfd,(struct sockaddr *)servaddr,sizeof(servaddr));
for(;;)
{
clilen=sizeof(cliaddr);
recvfrom(sockfd,buff,MAXSIZE,(struct sockaddr *)cliaddr,&clilen);
strcat(buff,”FROM THE SERVER”);
n=strlen(buff);
sendto(sockfd,buff,n,0,(struct sockaddr *)cliaddr,sizeof(cliaddr));
}
//udp echo client
#include
#include
#include
#define SERV_PORT 9999
#define MAXSIZE 1024
main()
{
int nbytes,sockfd;
char buff[MAXSIZE];
struct sockaddr_in servaddr,cliaddr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
nbytes=read(0,buff,MAXSIZE);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERV_PORT);
servaddr.sin_addr.s_addr=htonl(Inetaddr("172.16.5.80"));
sendto(sockfd,buff,nbytes,0,(struct sockaddr *)servaddr,sizeof(servaddr));
n=recvfrom(sockfd,buff,MAXSIZE,(struct sockaddr *)cliaddr,&sizeof(cliaddr));
write(1,buff,n);
}
3 comments:
Be obliged your material thanks for sharing with us...........
Networking training in chandigarh
Congratulations for this nice looking blog. All posts are super....
Ecommerce Web Designing Company Joomla Web Development Company Magento Web Designing Company
Your blogs are totally worth giving time and energy.
Immigration bradford
Family Lawyers bradford
Personal Injury Solicitor Bradford
Post a Comment