Professional Documents
Culture Documents
int main() {
return 0;
#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){
// 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;
}