Search This Blog

Showing posts with label OS. Show all posts
Showing posts with label OS. Show all posts

Using System calls in C Program to rename a Directory in Linux

C Program to rename a directory in Unix or Linux operating systems using system call rename().

#include<stdio.h>
main()
{

char s[10],d[10];
printf("Enter source Dir Name:\n");
scanf("%s",s);
printf("Enter New Dir Name:\n");
scanf("%s",d);
if(rename(s,d)==-1)
perror("Can't be changed\n");
else
printf("%s is changed to %s\n\n",s,d);
}

How to Create process and display Process ID (pid) of Both Parent and Child

This program shows how to create process and display Process ID (pid) of Both Parent and Child processes. The process is created using the fork system call in UNIX (Linux) operating systems.

#include<stdio.h>
#include<dirent.h>
main(int argc,char **argv)
{

int pid,i;
for(i=0;i<atoi(argv[1]);i++)
{
pid=fork();
if(pid==0)
{
printf("child process id %d Parent process
id %d\n",getpid(),getppid());
}}}

How to use fork and exec System Call

Using fork and exec System calls in Linux (UNIX) operating systems. Fork system call is used to create a child process from a parent process.
#include<stdio.h>
main()
{

int pid;
pid=fork();
printf("%d\n",pid);
if(pid==0)
{
exec("/bin/date\n",NULL,NULL);
exit(0); }
else
{
printf("Parent process %d\n",pid); }}
if(sp)
while(dd=readdir(sp))
printf("--> %s\n",dd->d_name);
}
}

How to use exit() System call

C program to show how to use exit system call. The function exit() is used to exit from a process (or to terminate a process). This works both in Linux or UNIX and Windows operating systems.

#include<stdio.h>
main()
{

int pid;
pid=fork();
printf("%d\n",pid);
if(pid<0)
{
perror("Child can't be executed\n");
exit(-1);
}
else
{
printf("Child created\n");
exit(0);
}
}