q1: C programme that accepts valid file names as command line arguments and for each of the arguments, prints the type of the file (regular file, directory file, character special file, block special file,
symbolic link, etc)
Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main(int argc,char *argv[])
{
int i;
struct stat buf;
for(i=1;i<argc;i++)
{
printf("%s",argv[i]);
if(lstat(argv[i],&buf)==-1)
{
printf(" Lstat error\n");
continue;
}
if(S_ISREG(buf.st_mode))
printf(" Regular file\n");
if(S_ISDIR(buf.st_mode))
printf(" Directory file\n");
if(S_ISCHR(buf.st_mode))
printf(" Character file\n");
if(S_ISBLK(buf.st_mode))
printf(" Block file\n");
if(S_ISLNK(buf.st_mode))
printf(" Symbolic link file\n");
}
}
/////////////////////////////////////////////////////
q2: C programme to do the following: Using fork() create a child process. The child process prints its own process id and id of its parent and then exits. The parent waits for the childto finish(by executing wait()) and prints its own process id and the id of its child and then exits.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
int main()
{
pid_t ppid,mpid,pid,status=0;
pid=fork();
if(pid<0)
{
printf("Error");
exit(0);
}
if(pid==0)
{
mpid=getpid();
printf("\n I am child, my id is %d",mpid);
ppid=getppid();
printf("\n I am parent, my id is %d ",ppid);
exit(1);
}
pid=waitpid(pid,&status,0);
mpid=getpid();
printf("\n I am parent with id %d and my child is %d \n",mpid,pid);
}
both giving sysntax error,where is the mistake,can any rectify it,i have tried[in fedora 12-linux] i couldnt got solution