Skip to content

lab3 - Process

Print
  1. Download Ubuntu 24.04.1 LTS : https://ubuntu.com/download/desktop/thank-you?version=24.04.1&architecture=amd64&lts=true
  2. ตรวจสอบค่า sum ของ ISO File ใน Powershell:
Powershell - Check SUM SHA256
Get-FileHash C:\Users\xxxx\Downloads\ubuntu-24.04.1-desktop-amd64.iso -Algorithm SHA256 | Format-List
  1. ติดตั้ง Ubuntu 24.04.1 LTS บท VirtualBox New VM
  2. ตรง Name ใส่ Ubuntu 24.04.1 New VM Ubuntu
  3. ตรง Folder ก็ตามค่าเริ่มต้นไป
  4. ตรง ISO Image กดปุ่ม Other SelectISO
  5. หาไฟล์ ISO ที่ Download มา Open ISO
  6. แล้วกด Open
  7. ติ๊กถูก Skip Unattended Installation Skip Unattended
  8. ตรง Hardware เปลี่ยน Base memory เป็น 4096 mb Ram 4096mb
  9. กดปุ่ม Finish แล้ว Virtual Box จะรันหน้าต่างขึ้นมา
  10. กด Enter เลือก *Try or Install Ubuntu BootVM
  11. จะโหลด Ubuntu ขึ้นมาดังรูป Setup Ubuntu
  12. ระบบจะติดตั้ง Ubuntu กด Next Setup Ubuntu2
  13. กด Next Setup Ubuntu3
  14. กด Next Setup Ubuntu4
  15. กด Next Setup Ubuntu5
  16. กด Next Setup Ubuntu6
  17. กด Next Setup Ubuntu7
  18. กด Next Setup Ubuntu8
  19. กด Next Setup Ubuntu9
  20. ตรง Your name ใสชื่อตัวเรา และตรง Password ให้ตั้งค่าตามที่เราต้องการ แล้วกด Next Setup Ubuntu10
  21. กด Next Setup Ubuntu11
  22. กด Install Setup Ubuntu12
  23. ระบบจะติดตั้ง Ubuntu Setup Ubuntu13
  24. ระบบจะให้เรา Reboot
  25. Login เข้ามาจะเจอหน้า Welcome ให้กด Next ไปเรื่อยๆ Welcome Ubuntu
  26. ให้กด Next Welcome Ubuntu
  27. ให้กด Next Welcome Ubuntu
  28. ให้กด Finish Welcome Ubuntu
  1. ไปที่ Terminal Find Terminal

  2. เปิด Terminal Open Terminal

  3. ติดตั้ง C/C++ Compiler

    Bash - Terminal
    sudo apt install build-essential
  4. พิมพ์ y แล้ว Enter Install c/c++

  5. ทำการ Check ว่า C/C++ Compiler ติดตั้งสมบูรณ์มั๊ยด้วย

    Bash - Terminal
    gcc

    ดูได้จากรูป Check c/c++

  1. ไปที่ VSCode Website
  2. กด Download VSCode เลือก .deb VSCode DEB
  3. รอโหลด VSCode Download VSCode
  4. Open File และ Install Install VSCode
  5. Open VSCode Open VSCode
  6. Install C/C++ Extension Pack Install C/C++ Extension Pack

สร้างไฟล์ใน VSCode แล้วลง Extension

create_process.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t p;
p=fork();
if(p==0) //child
{
printf("I am child having PID %d\n",getpid());
printf("My parent PID is %d\n",getppid());
}
else //parent
{
printf("I am parent having PID %d\n",getpid());
printf("My child PID is %d\n",p);
}
}

OUTPUT

Terminal window
I am parent having PID 9445
My child PID is 9446
I am child having PID 9446
My parent PID is 1835

create_orphan_process.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t p;
p=fork();
if(p==0)
{
sleep(5); //child goes to sleep and in the mean time parent terminates
printf("I am child having PID %d\n",getpid());
printf("My parent PID is %d\n",getppid());
}
else
{
printf("I am parent having PID %d\n",getpid());
printf("My child PID is %d\n",p);
}
}

OUTPUT

create_zombie_process.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t t;
t=fork();
if(t==0)
{
printf("Child having id %d\n",getpid());
}
else
{
printf("Parent having id %d\n",getpid());
sleep(15); // Parent sleeps. Run the ps command during this time
}
}

OUTPUT

Process : Prevent Zombie process by wait()

Section titled “Process : Prevent Zombie process by wait()”

prevent_zombie_process.c
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/wait.h>
int main()
{
pid_t p;
printf("before fork\n");
p=fork();
if(p==0)//child
{
printf("I am child having id %d\n",getpid());
printf("My parent's id is %d\n",getppid());
}
else//parent
{
wait(NULL);
printf("My child's id is %d\n",p);
printf("I am parent having id %d\n",getpid());
}
printf("Common\n");
}

OUTPUT

Prevent Zombie Process

write_shared_memory.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
/* creates shared memory segment with key 2345, having size 1024 bytes.
IPC_CREAT is used to create the shared segment if it does not exist.
0666 are the permisions on the shared segment */
shmid=shmget((key_t)2345, 1024, 0666|IPC_CREAT);
printf("Key of shared memory is %d\n",shmid);
//process attached to shared memory segment
shared_memory=shmat(shmid,NULL,0);
//this prints the address where the segment is attached with this process
printf("Process attached at %p\n",shared_memory);
printf("Enter some data to write to shared memory\n");
//get some input from user
read(0,buff,100);
//data written to shared memory
strcpy(shared_memory,buff);
printf("You wrote : %s\n",(char *)shared_memory);
}

OUTPUT

Write Shared Memory

read_shared_memory.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666);
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0); //process attached to shared memory segment
printf("Process attached at %p\n",shared_memory);
printf("Data read from shared memory is : %s\n",(char *)shared_memory);
}

OUTPUT

Read Shared Memory

send_queue.c
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#define MAX_TEXT 512 //maximum length of the message that can be sent allowed
struct my_msg{
long int msg_type;
char some_text[MAX_TEXT];
};
int main()
{
int running=1;
int msgid;
struct my_msg some_data;
char buffer[50]; //array to store user input
msgid=msgget((key_t)14534,0666|IPC_CREAT);
if (msgid == -1) // -1 means the message queue is not created
{
printf("Error in creating queue\n");
exit(0);
}
while(running)
{
printf("Enter some text:\n");
fgets(buffer,50,stdin);
some_data.msg_type=1;
strcpy(some_data.some_text,buffer);
if(msgsnd(msgid,(void *)&some_data, MAX_TEXT,0)==-1) // msgsnd returns -1 if the message is not sent
{
printf("Msg not sent\n");
}
if(strncmp(buffer,"end",3)==0)
{
running=0;
}
}
}

OUTPUT

Send Message Queue

read_queue.c
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
struct my_msg{
long int msg_type;
char some_text[BUFSIZ];
};
int main()
{
int running=1;
int msgid;
struct my_msg some_data;
long int msg_to_rec=0;
msgid=msgget((key_t)14534,0666|IPC_CREAT);
while(running)
{
msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_rec,0);
printf("Data received: %s\n",some_data.some_text);
if(strncmp(some_data.some_text,"end",3)==0)
{
running=0;
}
}
msgctl(msgid,IPC_RMID,0);
}

OUTPUT

Read Message Queue

producer.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/types.h>
int main(){
const int SIZE = 4096;
const char *name = "OS";
const char *message_0 = "Hello";
const char *message_1 = "World!";
int fd;
char *ptr;
fd = shm_open(name, O_CREAT|O_RDWR,0666);
ftruncate(fd, SIZE);
ptr = (char*)mmap(0, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
sprintf(ptr, "%s", message_0);
ptr += strlen(message_0);
sprintf(ptr, "%s", message_1);
ptr += strlen(message_1);
getchar();
return 0;
}
consumer.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/types.h>
int main(){
const int SIZE = 4096;
const char *name = "OS";
int fd;
char *ptr;
fd = shm_open(name, O_RDWR,0666);
ptr = (char*)mmap(0, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
printf("%s\n", (char *)ptr);
shm_unlink(name);
return 0;
}

OUTPUT

Terminal window
HelloWorld!