Rasmalai cakes
slip 1 a
/Program to create hole in file
#include<stdio.h>
#include<fcntl.h>
#include"string.h"
int main()
{
//create a new file by named as file.txt
int n=creat("file.txt","w");
char ch[16]="hello world how are";
char str[20]="od -c file.txt";
//change permission of file.txt with maximum access
system("chmod 777 file.txt");
//write "helloworld string in file.txt
write(n,ch,16);
// to move cursor from begging to 48th position
lseek(n,48,SEEK_SET);
//write "helloworld string in file.txt
write(n,ch,16);
// to prompt command in command prompt
system(str);
return(0);
}
slip 2b
/To handle the two-way communication between parent and child using pipe.
#include<stdio.h>
#include<unistd.h>
int main()
{
int pipefds1[2], pipefds2[2];
int returnstatus1, returnstatus2;
int pid;
char pipe1writemessage[20] = "Hi";
char pipe2writemessage[20] = "Hello";
char readmessage[20];
returnstatus1 = pipe(pipefds1);
if (returnstatus1 == -1) {
printf("Unable to create pipe 1 \n");
return 1;
}
returnstatus2 = pipe(pipefds2);
if (returnstatus2 == -1) {
printf("Unable to create pipe 2 \n");
return 1;
}
pid = fork();
if (pid != 0) // Parent process {
close(pipefds1[0]); // Close the unwanted pipe1 read side
close(pipefds2[1]); // Close the unwanted pipe2 write side
printf("In Parent: Writing to pipe 1 – Message is %s\n", pipe1writemessage);
write(pipefds1[1], pipe1writemessage, sizeof(pipe1writemessage));
read(pipefds2[0], readmessage, sizeof(readmessage));
printf("In Parent: Reading from pipe 2 – Message is %s\n", readmessage);
} else { //child process
close(pipefds1[1]); // Close the unwanted pipe1 write side
close(pipefds2[0]); // Close the unwanted pipe2 read side
read(pipefds1[0], readmessage, sizeof(readmessage));
printf("In Child: Reading from pipe 1 – Message is %s\n", readmessage);
printf("In Child: Writing to pipe 2 – Message is %s\n", pipe2writemessage);
write(pipefds2[1], pipe2writemessage, sizeof(pipe2writemessage));
}
return 0;
}
slip 3 q2#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
#include<sys/stat.h>
int main(int argc,char *argv[])
{
int i;
struct stat st;
for(i=0;i<argc;i++)
{
stat(argv[i],&st);
if(S_ISDIR(st.st_mode))
printf("\n type of the file %s is directory",argv[i]);
else if(S_ISREG(st.st_mode))
printf("\n type of the file is %s regular file",argv[i]);
else
printf(" other than directory or regular file");
}
return 0;
}
slip 6and 3 q1
/atexit()
#include <stdio.h>
#include <stdlib.h>
void functionA ()
{
printf("This is functionA\n");
}
int main ()
{
/* register the termination function */
atexit(functionA );
printf("Starting main program...\n");
printf("Exiting main program...\n");
return(0);
}
slip 5 q1
#include<stdio.h>
#include<sys/stat.h>
#include<string.h>
int main()
{
char fname[20];
struct stat buff;
printf("\n enter file to check size");
scanf("%s",fname);
stat(fname,&buff);
printf("\nsize of %s file is %ld byte",fname,buff.st_size);
return 0;
}
slip 6 q2
include<stdio.h>
#include<dirent.h>
#include<unistd.h>
int main()
{
DIR *dp;
int cnt=0;
dp=opendir(".");
struct dirent *dent;
while((dent=readdir(dp))!=NULL)
{
printf("\nfile=%s",dent->d_name);
cnt++;
}
printf("\n total number files is %d",cnt);
}
slip 1 q 2
#include<stdio.h>#include<sys/stat.h>
int main(int argc,char *argv[])
{
struct stat buff;
for(int i=1;i<argc;i++)
{
stat(argv[i],&buff);
printf("\n file=%s inode=%ld",argv[i],buff.st_ino);
}
0 Comments