0% found this document useful (0 votes)
9 views5 pages

Ex 2

The document contains several C programs demonstrating system calls in Unix-like operating systems. It includes examples of the fork system call, executing commands with execvp, working with directories, and retrieving file information using the stat system call. Each program is accompanied by error handling and prints relevant process or file information.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Ex 2

The document contains several C programs demonstrating system calls in Unix-like operating systems. It includes examples of the fork system call, executing commands with execvp, working with directories, and retrieving file information using the stat system call. Each program is accompanied by error handling and prints relevant process or file information.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Fork System Call program


#include <stdio.h>

#include <stdlib.h>

#include <unistd.h> // Include this header for fork(), getpid(), and getppid()

int main() {

int pid;

pid = fork(); // Fork system call

// Check if fork() failed

if (pid == -1) {

printf("\n CHILD PROCESS NOT CREATED\n");

exit(0); // Exit if fork fails

// If we're in the child process

if (pid == 0) {

printf("\n I AM CHILD PROCESS AND MY ID IS %d\n", getpid());

printf("\n THE CHILD PARENT PROCESS ID IS: %d\n", getppid());

// If we're in the parent process

else {

printf("\nI AM PARENT PROCESS AND MY ID IS %d\n", getpid());

printf("\nTHE PARENTS PARENT PROCESS ID IS: %d\n", getppid());

return 0;

}
2. System call with arguments program
#include <stdio.h>

#include <stdlib.h>

#include <unistd.h> // For fork and execvp

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

int pid, i;

pid = fork();

if (pid == -1) {

// Fork failed

printf("\n CHILD PROCESS NOT CREATED\n");

exit(1);

if (pid == 0) {

// Child process code

printf("\n CHILD PROCESS IS IN PROGRESS\n");

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

printf("\n THE CHILD PROCESSING VALUE IS: %d\n", i);

execvp("ls", argv); // Executes 'ls' command

// execvp replaces the current process image, so anything after this line won't be executed unless
execvp fails.

printf("\n EXECVP FAILED\n");

exit(0);

} else {

// Parent process code

printf("\n PARENT PROCESS IS WAITING\n");

// You can add wait() here to wait for the child process to finish.
wait(NULL);

printf("\n CHILD PROCESS COMPLETED ITS TASK\n");

return 0;

3. Working with directory program


#include <stdio.h>

#include <stdlib.h>

#include <dirent.h> // Correct header for directory operations

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

DIR *dir;

struct dirent *rddir;

// Check if directory argument is provided

if (argc != 2) {

printf("Usage: %s <directory_name>\n", argv[0]);

exit(1); // Exit if directory path is not provided

printf("LISTING THE DIRECTORY CONTENT\n");

// Open the directory

dir = opendir(argv[1]);

if (dir == NULL) {

perror("opendir"); // Print error if directory can't be opened

exit(1);

printf("THE CURRENT DIRECTORY FILES ARE:\n");


// Loop through directory entries and print them

while ((rddir = readdir(dir)) != NULL) {

printf("%s\n", rddir->d_name); // Print each file's name

closedir(dir); // Close the directory once done

return 0; // Return 0 to indicate successful execution

4. Stat system call program


#include <stdio.h> // For printf, perror

#include <sys/types.h> // For stat struct types

#include <sys/stat.h> // For struct stat

#include <time.h> // For ctime()

int main() {

struct stat sfile;

// Replace "stat.c" with the file you want to check

if (stat("stat.c", &sfile) == -1) {

perror("stat"); // If stat() fails, print the error

return 1; // Return an error code

// Print file information

printf("File Owner UID: %d\n", sfile.st_uid); // User ID

printf("File Owner GID: %d\n", sfile.st_gid); // Group ID

printf("File Size: %lld bytes\n", (long long) sfile.st_size); // File size

printf("File Blocks: %lld\n", (long long) sfile.st_blocks); // Blocks allocated


printf("File Inode Number: %ld\n", sfile.st_ino); // Inode number

// Convert timestamps to readable format

printf("Last Access Time: %s", ctime(&sfile.st_atime)); // Last accessed

printf("Last Modification Time: %s", ctime(&sfile.st_mtime)); // Last modified

printf("Last Status Change Time: %s", ctime(&sfile.st_ctime)); // Metadata change

return 0; // Success

You might also like