0% found this document useful (0 votes)
3 views

SystemCall Programs

The document contains three programs demonstrating the use of system calls in C. The first program copies a file, the second implements a move operation, and the third illustrates forking and executing processes. Each program includes error handling and basic file operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SystemCall Programs

The document contains three programs demonstrating the use of system calls in C. The first program copies a file, the second implements a move operation, and the third illustrates forking and executing processes. Each program includes error handling and basic file operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

//System call programs

//Program 1
//Copy a file to another using system calls
//Usage of command line arguments

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

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


{
char buf;
int fd_one, fd_two;

fd_one = open(argv[1], O_RDONLY);

if (fd_one == -1)
{
printf("Error opening first_file\n");
close(fd_one);
return;
}

fd_two = open(argv[2], O_WRONLY | O_CREAT);

while(read(fd_one, &buf, 1))


{
write(fd_two, &buf, 1);
}

printf("\nSuccessfully copied %12s to


%12s\n”,argv[1] , argv[2]);

close(fd_one);
close(fd_two);}
//mv implemented using system call
//Program 2
#include<fcntl.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
int main(int argc, char **argv)
{
int fd1,fd2;
int n,count=0;
fd1=open(argv[1],O_RDONLY);
fd2=creat(argv[2],S_IWUSR);
rename(fd1,fd2);
unlink(argv[1]);
return (0);
}
//Fork and exec system call
//Program 3

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

char str[128];

strcpy(str, "Hello");

printf("str's address is %p\n", str);

pid_t pid = fork();

if (pid == 0) { // The child should modify str

printf("I am the child. str's address is %p\n", str);

strcpy(str, "Howdy");

printf("I am the child and I changed str to %s. str's address is still %p\n", str, str); }

else { // The parent should sleep and print out str

printf("I am the parent. str's address is %p\n", str);

printf("I am the parent, and I'm going to sleep for 2 seconds.\n");

sleep(2);

printf("I am the parent. I just woke up. str's address is %p, and its value is %s\n", str, str);

return 0;

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

pid_t children[8]; for (size_t i = 0; i < 8; i++) {

if ((children[i] = fork()) == 0)

exit(110 + i); }

for (size_t i = 0; i < 8; i++) {

int status; pid_t pid = waitpid(children[i], &status, 0);

assert(pid == children[i]);

assert(WIFEXITED(status) && (WEXITSTATUS(status) == (110 + i)));

printf("Child with pid %d accounted for (return status of %d).\n", children[i],


WEXITSTATUS(status)); }

return 0;

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

printf("Starting the program\n");

pid_t pidOrZero1 = fork();

pid_t pidOrZero2 = fork();

if (pidOrZero1 != 0 && pidOrZero2 != 0) {

printf("Hello\n"); }

if (pidOrZero2 != 0) {

printf("Hi there\n");

return 0;

You might also like