You are on page 1of 18

1.

Write Programs using the following system calls of UNIX operating system: o fork, exec,
getpid, exit, wait, close, stat, opendir, readdir.

2.

#include<stdio.h>

#include<string.h>

#include<unistd.h>

#include<fcntl.h>

int main (void)

int fd[2];

char buf1[12] = "hello world";

char buf2[12];

// assume foobar.txt is already created

fd[0] = open("foobar.txt", O_RDWR);

fd[1] = open("foobar.txt", O_RDWR);

write(fd[0], buf1, strlen(buf1));

write(1, buf2, read(fd[1], buf2, 12));

close(fd[0]);

close(fd[1]);

return 0;

}
3) #include<stdio.h>

#include<stdlib.h>

#include<dirent.h>

#define DATA_SIZE 1000

void createf()

{ char data[DATA_SIZE];

char n[100];

FILE * fPtr;

int i;

printf("create 2 files \nfile1: with data \nfile2: without data for copying\n");

for ( i=0;i<2;i++){

printf("enter a file name:");

gets(n);

fPtr = fopen(n,"w");

if(fPtr == NULL)

{ printf("Unable to create file.\n");

exit(EXIT_FAILURE);

printf("Enter contents to store in file : \n");

fgets(data, DATA_SIZE, stdin);

fputs(data, fPtr);

fclose(fPtr);

printf("File created and saved successfully. ?? \n");

void copyfun(){

char ch, source_file[20], target_file[20];

FILE *source, *target;

printf("Enter name of file to copy\n");

gets(source_file);
source = fopen(source_file, "r");

if (source == NULL)

printf("Press any key to exit...\n");

exit(EXIT_FAILURE);

printf("Enter name of target file\n");

gets(target_file);

target = fopen(target_file, "w");

if (target == NULL)

fclose(source);

printf("Press any key to exit...\n");

exit(EXIT_FAILURE);

while ((ch = fgetc(source)) != EOF)

fputc(ch, target);

printf("File copied successfully.\n");

fclose(source);

fclose(target);

void lsandgrep(){

char fn[10],pat[10],temp[200];

FILE *fp;

char dirname[10];

DIR*p;

struct dirent *d;

printf("Enter directory name\n");

scanf("%s",dirname);

p=opendir(dirname);
if(p==NULL)

perror("Cannot find directory");

exit(0);

while(d=readdir(p))

printf("%s\n",d->d_name);

int main(){

createf();

copyfun();

lsandgrep();

}
10

#include<stdio.h>

#include<pthread.h>

#include<tgmath.h>

void* prime(void* p);

int pr(int n);

int main()

pthread_t id;

pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_create (&id,&attr,prime,NULL);

pthread_join(id,NULL);

int pri(int n)

int c=0;

int i;

for(i=1;i<=n;i++)

if (n%i==0)

c=c+1;

return c;

void *prime(void *p)

int i,n;
printf("Enter the number:\n");

scanf("%d",&n);

if (pri(n)<=2)

printf("%d is a prime number\n",n);

else

printf("%d is not a prime number\n",n);

pthread_exit(0);

9)

#include<stdio.h>

#include<sys/ipc.h>

#include<sys/shm.h>

#include<sys/types.h>

#include<string.h>

#include<errno.h>

#include<stdlib.h>

#include<unistd.h>

#include<string.h>

#define BUF_SIZE 1024


#define SHM_KEY 0x1234

struct shmseg {

int cnt;

int complete;

char buf[BUF_SIZE];

};

int fill_buffer(char * bufptr, int size);

int main(int argc, char *argv[]) {

int shmid, numtimes;

struct shmseg *shmp;

char *bufptr;

int spaceavailable;

shmid = shmget(SHM_KEY, sizeof(struct shmseg), 0644|IPC_CREAT);

if (shmid == -1) {

perror("Shared memory");

return 1;

// Attach to the segment to get a pointer to it.

shmp = shmat(shmid, NULL, 0);

if (shmp == (void *) -1) {

perror("Shared memory attach");

return 1;

/* Transfer blocks of data from buffer to shared memory */

bufptr = shmp->buf;

spaceavailable = BUF_SIZE;

for (numtimes = 0; numtimes < 5; numtimes++) {


shmp->cnt = fill_buffer(bufptr, spaceavailable);

shmp->complete = 0;

printf("Writing Process: Shared Memory Write: Wrote %d bytes\n", shmp->cnt);

bufptr = shmp->buf;

spaceavailable = BUF_SIZE;

sleep(3);

printf("Writing Process: Wrote %d times\n", numtimes);

shmp->complete = 1;

if (shmdt(shmp) == -1) {

perror("shmdt");

return 1;

if (shmctl(shmid, IPC_RMID, 0) == -1) {

perror("shmctl");

return 1;

printf("Writing Process: Complete\n");

return 0;

int fill_buffer(char * bufptr, int size) {

static char ch = 'A';

int filled_count;

//printf("size is %d\n", size);

memset(bufptr, ch, size - 1);

bufptr[size-1] = '\0';

if (ch > 122)


ch = 65;

if ( (ch >= 65) && (ch <= 122) ) {

if ( (ch >= 91) && (ch <= 96) ) {

ch = 65;

filled_count = strlen(bufptr);

//printf("buffer count is: %d\n", filled_count);

//printf("buffer filled is:%s\n", bufptr);

ch++;

return filled_count;

4)

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <error.h>

#include <signal.h>

#include <unistd.h>

#include <syslog.h>

int main()

{
FILE *getPIDS;

int i;

char line[130];

pid_t killpid;

// setuid to that of root//

pid_t mypid = getpid();

pid_t myppid = getppid();

getPIDS = popen("pidof -x yes","r");

while (fgets(line,sizeof line,getPIDS)) {

printf("KILL PID: %s",line);

kill(i,SIGKILL);

6)

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main()

// fork() Create a child process

int pid = fork();

if (pid > 0) {

//getpid() returns process id


// while getppid() will return parent process id

printf("Parent process\n");

printf("ID : %d\n\n", getpid());

else if (pid == 0) {

printf("Child process\n");

// getpid() will return process id of child process

printf("ID: %d\n", getpid());

// getppid() will return parent process id of child process

printf("Parent -ID: %d\n\n", getppid());

sleep(10);

// At this time parent process has finished.

// So if u will check parent process id

// it will show different process id

printf("\nChild process \n");

printf("ID: %d\n", getpid());

printf("Parent -ID: %d\n", getppid());

else {

printf("Failed to create child process");

return 0;

}
#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <unistd.h>

#include <errno.h>

int main ()

pid_t child_pid;

int child_status;

child_pid = fork ();

if (child_pid > 0) {

// parent process will sleep for 30 seconds and exit, without a call to wait()

fprintf(stderr,"parent process - %d\n", getpid());

sleep(30);

exit(0);

else if (child_pid == 0) {

// child process will exit immediately

fprintf(stderr,"child process - %d\n", getpid());


exit(0);

else if (child_pid == -1) {

// fork() error

perror("fork() call failed");

exit (-1);

else {

// this should not happen

fprintf(stderr, "unknown return value of %d from fork() call", child_pid);

exit (-2);

return 0;

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{

int pid = fork();


if (pid > 0) {

printf("Parent process\n");
printf("ID : %d\n\n", getpid());
}
else if (pid == 0) {
printf("Child process\n");

printf("ID: %d\n", getpid());

printf("Parent -ID: %d\n\n", getppid());


sleep(10);

printf("\nChild process \n");


printf("ID: %d\n", getpid());
printf("Parent -ID: %d\n", getppid());
}
else {
printf("Failed to create child process");
}

return 0;
}

5)

include <stdio.h>

#include <stdlib.h>

int main (void)

char ch;

FILE *filePointer;

filePointer = fopen("demo_text", "r");

if (filePointer == NULL)

printf("File is available \n");

else
{

while ((ch = fgetc(filePointer)) != EOF)

if (ch == '\r')

continue;

if (ch == '\n')

{ putchar (ch);

while ((ch = fgetc(filePointer)) == '\n' || ch == '\r') {}

if (ch != EOF) ungetc (ch, stdin);

else

break;

continue;

if (ch == ' ' || ch == '\t') { /* spaces & tabs */ putchar (' ');

while ((ch = fgetc(filePointer)) == ' ' || ch == '\t') {}

if (ch != EOF)

ungetc (ch, stdin);

else {

break;

continue;

putchar (ch);

return 0;

}
7 i)

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int main(){

printf("Sleeping for 5 seconds \n");

sleep(5);

printf("Sleep is now over \n");

ii)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void)
{
pid_t pid = fork();

if (pid == -1) {
perror("fork failed");
exit(EXIT_FAILURE);
}
else if (pid == 0) {
printf("Hello from the child process!\n");
_exit(EXIT_SUCCESS);
}
else {
int status;
(void)waitpid(pid, &status, 0);
}
return EXIT_SUCCESS;
}

8. int main() {

int pipefds[2];
int returnstatus;
char writemessages[2][20]={"Hi", "Hello"};
char readmessage[20];
returnstatus = pipe(pipefds);

if (returnstatus == -1) {
printf("Unable to create pipe\n");
return 1;
}

printf("Writing to pipe - Message 1 is %s\n", writemessages[0]);


write(pipefds[1], writemessages[0], sizeof(writemessages[0]));
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Reading from pipe – Message 1 is %s\n", readmessage);
printf("Writing to pipe - Message 2 is %s\n", writemessages[0]);
write(pipefds[1], writemessages[1], sizeof(writemessages[0]));
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Reading from pipe – Message 2 is %s\n", readmessage);
return 0;
}

You might also like