Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. OSL 1:
- cd, pwd, mkdir, rmdir, ls, man, touch, cat, rm, cp, mv, head, tail, more, less, history, chmod, cal
- -------------------------------------------------------
- 2. OSL 2:
- chmod, chown, chgrp, adduser, useradd, groupadd, userdel, groupdel, passwd, getent, gpasswd, umask
- -------------------------------------------------------
- 3. OSL 3:
- free, vmstat (-d, -s), top -h, /proc/meminfo, ps, free, df, kill
- -------------------------------------------------------
- 6. Zombie Process:
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- int main() {
- pid_t child_pid = fork();
- if (child_pid > 0) {
- // parent process
- printf("Parent process (PID %d) created child process with PID %d\n", getpid(), child_pid);
- sleep(5);
- } else if (child_pid == 0) {
- // child process
- printf("Child process (PID %d) exiting\n", getpid());
- exit(0);
- } else {
- // fork failed
- printf("Fork failed\n");
- exit(1);
- }
- return 0;
- }
- ----
- Orphan Process:
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- int main() {
- pid_t child_pid = fork();
- if (child_pid > 0) {
- // parent process
- printf("Parent process (PID %d) sleeping\n", getpid());
- sleep(10);
- } else if (child_pid == 0) {
- // child process
- printf("Child process (PID %d) created\n", getpid());
- sleep(5);
- printf("Child process (PID %d) exiting\n", getpid());
- } else {
- // fork failed
- printf("Fork failed\n");
- exit(1);
- }
- return 0;
- }
- ----------
- Parent & child process:
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- int main() {
- pid_t child_pid = fork();
- if (child_pid > 0) {
- // parent process
- printf("Parent process (PID %d) created child process with PID %d\n", getpid(), child_pid);
- sleep(5);
- printf("Parent process (PID %d) exiting\n", getpid());
- } else if (child_pid == 0) {
- // child process
- printf("Child process (PID %d) created\n", getpid());
- sleep(10);
- printf("Child process (PID %d) exiting\n", getpid());
- } else {
- // fork failed
- printf("Fork failed\n");
- exit(1);
- }
- return 0;
- }
- ----------------------------------------------------------------
- 7. Use of fork, sleep, kill, getpid and getppid:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- int main() {
- pid_t pid1, pid2, child_status;
- // Create first child process
- pid1 = fork();
- if (pid1 == 0) {
- // Code executed by child process
- printf("Child process 1 created with ID: %d\n", getpid());
- sleep(4);
- printf("Child process 1 parent ID: %d\n", getppid());
- } else {
- // Code executed by parent process
- waitpid(pid1, &child_status, 0);
- printf("Child process 1 with ID %d has terminated\n", pid1);
- // Create second child process
- pid2 = fork();
- if (pid2 == 0) {
- // Code executed by child process
- printf("Child process 2 created with ID: %d\n", getpid());
- printf("Child process 2 parent ID: %d\n", getppid());
- } else {
- // Kill second child process
- kill(pid2, SIGKILL);
- waitpid(pid2, &child_status, 0);
- printf("Child process 2 with ID %d has been killed\n", pid2);
- }
- }
- return 0;
- }
- ---------------------------------------------------------------------------
- 8. One Way Pipe:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #define MSGSIZE 16
- int main() {
- char msg[MSGSIZE] = "Hello, World!";
- char buffer[MSGSIZE];
- int p[2];
- if (pipe(p) < 0) {
- printf("Pipe creation failed\n");
- exit(1);
- }
- pid_t pid = fork();
- if (pid < 0) {
- printf("Fork failed\n");
- exit(1);
- }
- if (pid > 0) {
- // parent process
- close(p[0]);
- write(p[1], msg, MSGSIZE);
- printf("Parent process sent message: %s\n", msg);
- close(p[1]);
- } else {
- // child process
- close(p[1]);
- read(p[0], buffer, MSGSIZE);
- printf("Child process received message: %s\n", buffer);
- close(p[0]);
- }
- return 0;
- }
- ---------------------------------------------------------------------------------
- 9. Two Way PIPE
- sender.c:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #define MSGSIZE 16
- int main() {
- char msg[MSGSIZE];
- int p[2];
- if (pipe(p) < 0) {
- printf("Pipe creation failed\n");
- exit(1);
- }
- pid_t pid = fork();
- if (pid < 0) {
- printf("Fork failed\n");
- exit(1);
- }
- if (pid > 0) {
- // parent process (sender)
- close(p[0]);
- printf("Enter a message: ");
- fgets(msg, MSGSIZE, stdin);
- write(p[1], msg, MSGSIZE);
- close(p[1]);
- } else {
- // child process (receiver)
- close(p[1]);
- char buffer[MSGSIZE];
- read(p[0], buffer, MSGSIZE);
- printf("Received message: %s", buffer);
- close(p[0]);
- }
- return 0;
- }
- receiver.c:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #define MSGSIZE 16
- int main() {
- char msg[MSGSIZE];
- int p[2];
- if (pipe(p) < 0) {
- printf("Pipe creation failed\n");
- exit(1);
- }
- pid_t pid = fork();
- if (pid < 0) {
- printf("Fork failed\n");
- exit(1);
- }
- if (pid > 0) {
- // parent process (sender)
- close(p[0]);
- printf("Enter a message: ");
- fgets(msg, MSGSIZE, stdin);
- write(p[1], msg, MSGSIZE);
- close(p[1]);
- } else {
- // child process (receiver)
- close(p[1]);
- char buffer[MSGSIZE];
- read(p[0], buffer, MSGSIZE);
- printf("Received message: %s", buffer);
- printf("Enter a response: ");
- fgets(msg, MSGSIZE, stdin);
- write(p[1], msg, MSGSIZE);
- close(p[0]);
- }
- return 0;
- }
- -------------------------------------------------------------------------------
- 10. OSL 10: ONE WAY COMMUNICATION B/W TWO PROCESSES USING FIFO
- - sender:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- int main()
- {
- int fd;
- char * myfifo = "/tmp/myfifo";
- mkfifo(myfifo, 0666); // create the FIFO with read/write permissions
- char message[100];
- printf("Enter message: ");
- fgets(message, 100, stdin);
- fd = open(myfifo, O_WRONLY); // open the FIFO for writing
- write(fd, message, strlen(message)+1); // write the message to the FIFO
- close(fd);
- unlink(myfifo); // remove the FIFO
- return 0;
- }
- - receiver:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- int main()
- {
- int fd;
- char * myfifo = "/tmp/myfifo";
- mkfifo(myfifo, 0666); // create the FIFO with read/write permissions
- char message[100];
- fd = open(myfifo, O_RDONLY); // open the FIFO for reading
- read(fd, message, 100); // read the message from the FIFO
- printf("Received message: %s\n", message);
- close(fd);
- unlink(myfifo); // remove the FIFO
- return 0;
- }
- --------------------------------------------------------------------------------------------------------------------
- OSL 11: TWO WAY COMMUNICATION B/W TWO PROCESSES USING FIFO
- sender.c -
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- int main()
- {
- int fd;
- char *fifo = "/tmp/myfifo";
- mkfifo(fifo, 0666);
- char arr1[80], arr2[80];
- while (1)
- {
- printf("Waiting for input from User2...\n");
- fd = open(fifo, O_WRONLY);
- printf("User1: ");
- fgets(arr2, 80, stdin);
- write(fd, arr2, sizeof(arr2));
- close(fd);
- fd = open(fifo, O_RDONLY);
- read(fd, arr1, sizeof(arr1));
- printf("User2: %s\n", arr1);
- close(fd);
- }
- return 0;
- }
- receiver.c -
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- int main()
- {
- int fd;
- char *fifo = "/tmp/myfifo";
- mkfifo(fifo, 0666);
- char arr1[80], arr2[80];
- while (1)
- {
- printf("Waiting for input from User1...\n");
- fd = open(fifo, O_RDONLY);
- read(fd, arr1, sizeof(arr1));
- printf("User1: %s\n", arr1);
- close(fd);
- fd = open(fifo, O_WRONLY);
- printf("User2: ");
- fgets(arr2, 80, stdin);
- write(fd, arr2, sizeof(arr2));
- close(fd);
- }
- return 0;
- }
- ------------------------------------------------------------------------------------------------------------------------
- OSL 12: DEMONSTRATE WAIT AND SLEEP SYSTEM CALLS
- sleep():
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- int main() {
- printf("Before sleep\n");
- sleep(5); // sleep for 5 seconds
- printf("After sleep\n");
- return 0;
- }
- wait():
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- int main() {
- pid_t pid;
- pid = fork();
- if (pid == 0) { // child process
- printf("Child process is running\n");
- sleep(5); // child process sleeps for 5 seconds
- exit(0);
- } else if (pid > 0) { // parent process
- printf("Parent process is waiting for child process to terminate\n");
- wait(NULL); // parent process waits for child process to terminate
- printf("Child process terminated\n");
- } else { // error
- fprintf(stderr, "Fork failed\n");
- exit(1);
- }
- return 0;
- }
- -----------------------------------------------------------------------------------------------------------------------
- OSL 13: BASIC THREAD OPERATIONS
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- void *print_hello(void *arg) {
- printf("Hello from thread %ld\n", (long)arg);
- pthread_exit(NULL);
- }
- int main() {
- pthread_t thread_id;
- int rc;
- // create a new thread
- rc = pthread_create(&thread_id, NULL, print_hello, (void *)1);
- if (rc) {
- printf("ERROR; return code from pthread_create() is %d\n", rc);
- exit(-1);
- }
- // wait for the thread to complete
- rc = pthread_join(thread_id, NULL);
- if (rc) {
- printf("ERROR; return code from pthread_join() is %d\n", rc);
- exit(-1);
- }
- printf("Thread %ld has completed\n", (long)thread_id);
- pthread_exit(NULL);
- }
- ------------------------------------------------
- OPTIONAL THREAD OPS:
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- pthread_mutex_t mutex;
- void *print_message(void *ptr);
- int main() {
- pthread_t thread1, thread2;
- char *message1 = "Thread 1";
- char *message2 = "Thread 2";
- int rc1, rc2;
- // initialize mutex
- pthread_mutex_init(&mutex, NULL);
- // create thread 1
- printf("Creating thread 1...\n");
- rc1 = pthread_create(&thread1, NULL, print_message, (void*) message1);
- if (rc1) {
- printf("Error: return code from pthread_create() is %d\n", rc1);
- exit(-1);
- }
- // detach thread 1
- printf("Detaching thread 1...\n");
- rc1 = pthread_detach(thread1);
- if (rc1) {
- printf("Error: return code from pthread_detach() is %d\n", rc1);
- exit(-1);
- }
- // create thread 2
- printf("Creating thread 2...\n");
- rc2 = pthread_create(&thread2, NULL, print_message, (void*) message2);
- if (rc2) {
- printf("Error: return code from pthread_create() is %d\n", rc2);
- exit(-1);
- }
- // join thread 2
- printf("Joining thread 2...\n");
- rc2 = pthread_join(thread2, NULL);
- if (rc2) {
- printf("Error: return code from pthread_join() is %d\n", rc2);
- exit(-1);
- }
- // lock mutex and print final message
- printf("Locking mutex...\n");
- pthread_mutex_lock(&mutex);
- printf("Main thread and thread 2 are done!\n");
- pthread_mutex_unlock(&mutex);
- // destroy mutex
- printf("Destroying mutex...\n");
- pthread_mutex_destroy(&mutex);
- printf("Main thread exiting...\n");
- pthread_exit(NULL);
- }
- void *print_message(void *ptr) {
- char *message = (char*) ptr;
- // lock mutex
- printf("Locking mutex...\n");
- pthread_mutex_lock(&mutex);
- printf("%s\n", message);
- // unlock mutex
- printf("Unlocking mutex...\n");
- pthread_mutex_unlock(&mutex);
- pthread_exit(NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement