Wednesday, July 23, 2008

NETWORK & OS LAB

read() system call


#include fcntl.h
#include stdio.h
main()
{
int fd;
char ch[100];

fd=open("a.txt",O_RDONLY);
read(fd,&ch,7);
printf("%s",ch);
printf("\n");

}
program to create and write to a file using open() and write() system calls

#include
#include
main()
{
int fd;
fd=open("a.txt",O_CREAT|O_WRONLY,0644);
write(fd,"shibu\n",7);

}


fork() system call

#include unistd.h
#include stdio.h
main()
{
int i,j,pid,n;
printf("enter the delay:");
scanf("%d",&n);
pid=fork();
if (pid==0)/*child process*/
{
for(i=0;i<=100;i+=2)
{
printf("executing child %d\n",i);
}
}
else
{

for (i=1;i<=100;i++)
{
printf("executing parent %d\n",i);
}
}
}


exec() system call


#include unistd.h
#include stdio.h
main()
{
printf("before calling exec\n");
execl("/bin/date","date",NULL);
printf("after calling exec\n");
}

program to create a file using open() and write() system calls


#include fcntl.h
#include stdio.h
main()
{
int fd;
fd=open("a.txt",O_CREAT|O_WRONLY,0644);
write(fd,"shibu\n",7);

}

pipe() system call

#include
#include
void do_parent(int []);
void do_child(int []);
main()
{
int data_pipe[2];
int pid;
int rc;
rc = pipe(data_pipe);
printf("%d %d\n",data_pipe[0],data_pipe[1]);
if (rc == -1) { perror("pipe"); exit(1); }
pid = fork();
switch (pid)
{
case -1:
perror("fork"); exit(1);
perror("fork");
case 0:
do_child(data_pipe);
default:
do_parent(data_pipe);
}
}

void do_parent(int data_pipe[])
{
int c; /* data received from the user. */
int rc;
/* first, close the un-needed read-part of the pipe. */
close(data_pipe[0]);
while ((c = getchar()) > 0)
{
rc = write(data_pipe[1], &c, 1);
if (rc ==-1)
{
perror("Parent:write");close(data_pipe[1]);exit(1);
}
}
close(data_pipe[1]);

}
void do_child(int data_pipe[]) {
int c; /* data received from the parent. */
int rc;
/* first, close the un-needed write-part of the pipe. */
close(data_pipe[1]);
while ((rc = read(data_pipe[0], &c, 1)) > 0)
{
putchar(c);
}

}

SENDING "Hello World" through the pipe

#include stdio.h
#include unistd.h
#include sys/types.h
#include string.h
#include stdlib.h

int main(void)
{
int fd[2], nbytes;
pid_t childpid;
char string[] = "Hello, world!\n";
char readbuffer[80];

pipe(fd);

if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}

if(childpid == 0)
{
/* Child process closes up input side of pipe */
close(fd[0]);

/* Send "string" through the output side of pipe */
write(fd[1], string, (strlen(string)+1));
exit(0);
}
else
{
/* Parent process closes up output side of pipe */
close(fd[1]);

/* Read in a string from the pipe */
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("Received string: %s", readbuffer);
}

return(0);
}
if you want to know more about pipes click HERE

program for counting the number of words in the output when executing the command ls
#include stdio.h
#include stdlib.h
#include unistd.h

int main()
{
int pfds[2];

pipe(pfds);

if (!fork()) {
close(1); /* close normal stdout */
dup(pfds[1]); /* make stdout same as pfds[1] */
close(pfds[0]); /* we don't need this */
execlp("ls", "ls", NULL);
} else {
close(0); /* close normal stdin */
dup(pfds[0]); /* make stdin same as pfds[0] */
close(pfds[1]); /* we don't need this */
execlp("wc", "wc", "-l", NULL);
}
}

program implementing "ls|more" using pipes
#include
#include
main()
{
int fd[2];
pipe(fd);
switch(fork()){
case -1:perror("fork");
break;
case 0:close(1);
dup(fd[1]);
close(0);
execlp("ls","ls","-l",NULL);
break;
default:close(0);
dup(fd[0]);
execlp("more","more",NULL);
break;
}
}

SHARED MEMORY CLICK HERE

No comments: