Os 1-3 Programs
Os 1-3 Programs
AIM:
To study about the basics of UNIX
UNIX:
It is a multi-user operating system. Developed at AT & T Bell Industries, USA in
1969.
LINUX:
It is similar to UNIX, which is created by Linus Torualds. All UNIX
commands works in Linux. Linux is a open source software. The main feature
of Linux is coexisting with other OS such as windows and UNIX.
STRUCTURE OF A LINUXSYSTEM:
It consists of three parts.
a) UNIX kernel
b) Shells
c) Tools and Applications
UNIX KERNEL:
Kernel is the core of the UNIX OS. It controls all tasks, schedule all
Processes and carries out all the functions of OS.
SHELL:
Shell is the command interpreter in the UNIX OS. It accepts
command from the user and analyses and interprets them
pwd – Print Working Directory
cd – Change Directory
rm – Remove File
Deletes a file.
Example: rm oldfile.txt
ps – Process Status
2. can you write in one c programme Write programs using the following UNIX
operating system calls fork, exec, getpid, exit, wait, close, stat, opendir and
readdir.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
int main() {
pid_t pid;
int status;
if (pid == 0) {
// Child process
printf("Child process created, PID: %d\n", getpid());
// If exec fails
perror("exec failed");
exit(EXIT_FAILURE);
} else {
// fork() failed
perror("fork failed");
exit(EXIT_FAILURE);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
char buffer[1024];
ssize_t bytes;
while ((bytes = read(source_fd, buffer, sizeof(buffer))) > 0) {
write(dest_fd, buffer, bytes);
}
close(source_fd);
close(dest_fd);
printf("File copied from %s to %s\n", src, dest);
}
closedir(dir);
}
char line[1024];
while (fgets(line, sizeof(line), file)) {
if (strstr(line, pattern)) {
printf("%s", line);
}
}
fclose(file);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s command [arguments]\n", argv[0]);
printf("Commands:\n");
printf(" cp <source> <destination>\n");
printf(" ls <directory_path>\n");
printf(" grep <pattern> <filename>\n");
return 1;
}
if (strcmp(argv[1], "cp") == 0) {
if (argc != 4) {
printf("Usage: %s cp <source> <destination>\n", argv[0]);
return 1;
}
simulate_cp(argv[2], argv[3]);
} else if (strcmp(argv[1], "ls") == 0) {
const char *path = (argc == 3) ? argv[2] : ".";
simulate_ls(path);
} else if (strcmp(argv[1], "grep") == 0) {
if (argc != 4) {
printf("Usage: %s grep <pattern> <filename>\n", argv[0]);
return 1;
}
simulate_grep(argv[2], argv[3]);
} else {
printf("Unknown command: %s\n", argv[1]);
return 1;
}
return 0;
}