Showing posts with label Unix programming. Show all posts
Showing posts with label Unix programming. Show all posts

CREATING CHILD PROCESS USING FORK

CREATING CHILD PROCESS USING FORK IN UNIX PROGRAMS

#include
#include
#include
#include
#include
main()
{
int pid,m=0;
pid=fork();
if(pid==0)
{

printf("%d\n",m);
printf("CHILD PROCESS STARTED :");
printf("The child process ID 1: %d\n",getpid());
printf("The parent of child process ID 2: %d \n",getppid());
}
else
{
sleep(5);
printf("%d\n",m);
printf("PARENT PROCESS STARTED :");
printf("The parent process ID 3:%d \n",getpid());
printf("The Parent of Parent process ID 4: %d \n",getppid());
}
}

Program to find File types

FINDING FILE TYPES USING UNIX PROGRAMS

#include
#include
#include
#include
#include
main()
{
char fname[30];
int i,ch;
struct stat buf;
do
{
printf("\n Enter the filename: \n");
scanf("%s",fname);
i=lstat(fname,&buf);

if(i==0)
{

if(S_ISREG(buf.st_mode))
printf("\n It is a Regular File");
if(S_ISDIR(buf.st_mode))
printf("\n It is a Directory File");
if(S_ISCHR(buf.st_mode))
printf("\n It is a Character File");
if (S_ISBLK(buf.st_mode))
printf("\n It is a Block File");
if(S_ISFIFO(buf.st_mode))
printf("\n It is a Fifo File");
if(S_ISLNK(buf.st_mode))
printf("\n It is a Link File");
if(S_ISSOCK(buf.st_mode))
printf("\n It is a Socket File");

}

printf(“DO YOU WANT TO CONTINUE:\n”);
scanf(“%d”,&ch);
}
while(ch!=0);
}

FILE OPERATIONS USING UNIX PROGRAMMING

FILE OPERATIONS USING UNIX PROGRAMMING

#include
#include
#include
#include
#include

main()
{
int fd,ch,n,len,p;
char name[20],str[50],buf[40];
do
{
printf("Menu\n");
printf("1.Create a file\n");
printf("2.Open a file\n");
printf("3.Writing to a file\n");
printf("4.Reading from a file\n");
printf("5.Exit\n");
printf("Enter the choice[1-4]:\n");
scanf("%d",&ch);

switch(ch)
{
case 1:
printf("Enter the filename:\n");
getchar();
gets(name);
fd=creat(name,S_IRWXU);
printf("File Created\n");
close(fd);
break;
case 2:
printf("Enter the filename to open:\n");
getchar();
gets(name);
fd=open(name,O_RDWR);
printf("%d",fd);
if(fd!=3)
{
fd=open(name,O_CREAT O_RDWR);
printf("File is created\n");
}
else
printf("File Already Exist\n");
close(fd);
break;
case 3:
printf("Enter the filename to Write:\n");
getchar();
gets(name);
fd=open(name, O_RDWR);
printf("X=%d",fd);
if (fd==3)
{
printf("File Already Exist\n");
printf("Are you want to Append or overwrite:\n");
printf("0-Append\n");
printf("1-Overwrite\n");
printf("Enter your Choice[1-0]:");
scanf("%d",&ch);
if(ch==0)
lseek(fd,0,SEEK_END);
else
lseek(fd,0,SEEK_SET);
}
else
{
fd=open(name,O_CREAT O_RDWR,S_IRWXU);
printf("New File is created\n");
}
printf("Enter the String to Write\n");
scanf("%s",buf);
len=strlen(buf);
write(fd,buf,len);
close(fd);
break;
case 4:
printf("Enter the filename to read the content:\n");
getchar();
gets(name);
fd=open(name,O_RDWR);
if(fd==3)
{
printf("already exist!\n");
printf("From where you want to read :\n 0-Starting position \n (1-n)-other postion :\n");
scanf("%d",&p);
lseek(fd,p,SEEK_SET);
n=read(fd,buf,10);
buf[n]='\0';
printf("The content of the file is...%s\n",buf);
}
else
{
fd=open(name,O_CREAT O_RDWR,S_IRWXU);
printf("Newly Created No data to Read!");
}
close(fd);
break;
}
}
while(ch<=4);
}