You are on page 1of 5

CO_3 programs

1) // reverse the file using lseek() systemcall


#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>

int main() {

int fd1, fd2; //file descriptors


char c; //holds read char
int offset; //current offset

fd1 = open("foo.txt", O_RDONLY); //open file to read


if (fd1 < 0) {
printf("%s", "Open error");
}

fd2 = open("foorev.txt", O_WRONLY | O_CREAT, 0670); //open file to write with


rwxrw----
if(fd2 < 0) {
printf("%s", "Open error");
}

offset = lseek(fd1, 0, SEEK_END); //go to the end of the file

while (offset > 0) { //while it is not the beginning of the file


read(fd1, &c, 1); //read a char
write(fd2, &c, 1); //write the char
lseek(fd1, -2, SEEK_CUR); //go back 2 spots to the char before the one just read
offset--; //track the current offset
}

close(fd1); //close the files


close(fd2);

return 0;

2) //Reverse the file using fseek()

#include <stdio.h>
int main()
{
FILE *fp1,*fp2;
char ch;
long chLen;
fp1 = fopen("foo.txt", "r");
fp2=fopen("foo1.txt","w");
// Sets cursor at end of file.
fseek(fp1, 0L, SEEK_END);
chLen = ftell(fp1); // Gets the length of file.
// Sets cursor at last character of file.
fseek(fp1, -1L, SEEK_CUR);
while (chLen > 0)
{
ch = fgetc(fp1);
fputc(ch,fp2);
chLen--;
// Move cursor 2 characters before cursor.
fseek(fp1, -2L, SEEK_CUR);
}
fclose(fp1);
fclose(fp2);
printf("file copied in reverse order\n");
return 0;

CO-4 programs

/* Vfork () */

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int glob = 6; /* external variable in initialized data */
char buf[] = "a write to stdout\n";

int main(void)
{
int var; /* automatic variable on the stack */
int pid;
var = 88;
if (write(STDOUT_FILENO, buf, sizeof(buf)-1) != sizeof(buf)-1)
printf("write error");
printf("before fork\n"); /* we don't flush stdout */
if ((pid = vfork()) < 0) {
printf("fork error");
} else if (pid == 0) { /* child */
glob++; /* modify variables */
var++;
_exit(0);
}
else
{
}
printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var);
printf("userid=%d",getuid());
exit(0);
}

/* Orphan */

#include <stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
main()
{
int pid ;
printf(" PID %d and PPID %d.\n", getpid(),getppid()) ;
pid = fork() ;
if ( pid != 0 )
{
printf("PID %d and PPID %d.\n",getpid(), getppid()) ;
printf("My child's PID is %d\n", pid ) ;
}
else
{
sleep(10);
printf("PID %d and PPID %d.\n",getpid(), getppid()) ;
}
printf ("PID %d terminates.\n", getpid()) ;
}

/* orphan */
#include<stdio.h>
#include<unistd.h>
int main()
{
int p;
p=fork();
if(p==0) {
printf("the child process pid is %d and parent pid is %d\n",getpid(),getppid());
sleep(10);
}
printf("The process pid is %d parent pid %d\n", getpid(), getppid());
sleep(40);
printf("\nProcess %d is done its Parent pid %d...\n", getpid(), getppid());

return 0;
}

/* Zombie Process */

#include <stdio.h>
main ( )
{
int pid ;
pid = fork(); /* Duplicate. Child and parent continue from here */
if ( pid != 0 ) /* pid is non-zero, so I must be the parent */
{
while (1) /* Never terminate and never execute a wait ( ) */
sleep (100) ; /* stop executing for 100 seconds */
}
else /* pid is zero, so I must be the child */
{
exit (42) ; /* exit with any number */
}
}
The output is:
i> ./a.out & //execute the program in the background
[1] 5186
i> ps //obtain process status
PID TT STAT TIME COMMAND
5187 p0 Z 0:00 <exiting> the zombie child process
5149 p0 S 0:01 -csh (csh) the shell
5186 p0 S 0:00 a.out the parent process
5188 p0 R 0:00 ps
> kill 5186 //kill the parent process
[1] Terminated a.out
> ps //notice that the zombie is gone now
PID TT STAT TIME COMMAND
5149 p0 S 0:01 -csh (csh)
5189 p0 R 0:00 ps

//usage of wait ()
// return sum of 2 numbers to the parent process

#include<stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include<stdlib.h>
int main(void){

int pid;int a=20,b=30,c;


int status;
if(fork()){
/* wait for child, getting PID */
pid=wait(&status);
printf("I'm the parent.\n");
printf("pid=%d\n",pid);
if(WIFEXITED(status))
printf("sum=%d\n",WEXITSTATUS(status));
}
else
{
printf("I'm the child.\n");
c=a+b;
exit(c);
}
return 0;
}

// usage of execlp()

#include<stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void){

int child_pid;
int *status=NULL;
if(fork()){
/* wait for child, getting PID */
printf("I'm the parent.\n");
} else
{
execlp("ls","ls","-l",(char *)0);
}
return 0;
}

You might also like