0% found this document useful (0 votes)
31 views21 pages

0S Labs Inclass Guide

The document provides instructions for a lab assignment on operating systems and C programming. It includes links to tutorials on installing virtual machines, Linux commands, programming in C, and using makefiles. It also outlines tasks for submitting code and discusses processes, threads, and synchronization.

Uploaded by

Tomioka Giyuu
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)
31 views21 pages

0S Labs Inclass Guide

The document provides instructions for a lab assignment on operating systems and C programming. It includes links to tutorials on installing virtual machines, Linux commands, programming in C, and using makefiles. It also outlines tasks for submitting code and discusses processes, threads, and synchronization.

Uploaded by

Tomioka Giyuu
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/ 21

•Tất cả các em ghi họ tên & MSSV lên ô

chat của Google Meet.


Lab 1:
•Cài Virtual Machine
•Cài Ubuntu, Fedora, Kali, k8, CentOS
- Setup VM: https://youtu.be/wX75Z-4MEoM
- Virtual Box:
https://www.virtualbox.org/
- Ubuntu 18/20:
https://ubuntu.com/download/desktop
- Linux command:
Introduction to Linux and Basic L…
- Compile C program in Ubuntu:
https://youtu.be/oLjN6jAg-sY

Content:
- Practice with vim - text editor.
- Programming with C language.
- Compile a program with Makefile.
- How to retrieve the information of
running process? How is PCB table
controlled by OS?
- Video tutorial for vim:
https://youtu.be/wlR5gYd6um0
- Create a program with multiple
processes.

- C Programming:
C Programming Tutorial 1 - Intro …

- Advance Linux Programming:


http://www.cse.hcmut.edu.vn/~hungnq
/courses/nap/alp.pdf
-
- C programming Linux video:
C Programming Tutorial 6 - Intro to UNIX/Linux - Part 1
C Programming Tutorial 7 - Intro to UNIX/Linux - Part 2

- Linux command line interface: Linux Tutorial - Basic Command Line

- C tutorial: https://www.tutorialspoint.com/cprogramming/index.htm
- GNU make: https://www.gnu.org/software/make/manual/make.html
- How to use make: C for Beginners: Creating makefiles for C Programs using CLAN…
- C datatype: https://www.tutorialspoint.com/cprogramming/c_data_types.htm
- GNU GCC compilation:
How to Compile and Run C program Using GCC on Ubuntu 18.04 LTS (Linux) / Ub…
- Làm Lab 1 - Test 1
- Nộp toàn bộ source code của Lab 1 - Programming code. Nén lại 1 file .zip/.tar.gz và nộp
lên BKel.

Makefile: UPEvent: GCC and Makefiles

Create new child process


Shell Scripting Tutorial:
Shell Scripting Tutorial | Shell Scripting Crash Course | Linux Certification Training | Edureka

Git & GitHub Crash Course For Beginners


- Video Git: Git & GitHub Crash Course For Beginners

Shell Scripting Tutorial


- https://www.tutorialspoint.com/unix/shell_scripting.htm
-

Kafka & Concluent:


https://docs.confluent.io/platform/current/quickstart/ce-docker-quickstart.html

-
Create child process: fork()
- Linux fork() Introduction
THOẠI LÊ NGUYỄN HUYỀN 13:19
Lê Nguyễn Huyền Thoại - 2012122 trả lời tốt.
THỨC NGUYỄN TRÌNH13:19
Nguyễn Trình Thức - 2014694 hiểu chương trình.

Pre-view:
THÔNG TIN ĐĂNG KÝ ĐỀ TÀI BÀI TẬP MÔN HỌC HĐH HK221/2022-2023

Tasks: 23.01.2022:
- Làm bài Lab 1 - Test 2.
- Codepost.io: https://codepost.io/signup/join?code=8KDCWAN2AN
- Nộp Lab 1: Problem 4 (lập trình) vào CodePost.io
- Problem 4 Given a file named "numbers.txt" containing multiple lines of text.
Each line is a non-negative integer. Write a C program that reads integers
listed in this file and stores them in an array (or linked list). The program
then uses the fork() system call to create a child process. The parent
process will count the numbers of integers in the array that are divisible by
2. The child process will count numbers divisible by 3. Both processes then
send their results to the stdout. For examples, if the file "numbers.txt"
contains the following lines
- Tất cả 3. EXERCISES còn lại submit lên BKel, Bài Lab 1.

Book: The Linux Programming Interface


- Link: https://man7.org/tlpi/index.html
- Một số ví dụ khác về gọi fork() system call tạo process, khảo sát
dữ liệu chia sẻ idata.
CodePost.io: Autograde
Kết quả:

Process states

Zombie, orphan process:


https://www.tutorialspoint.com/zombie-and-orphan-processes-in-linux
Lab 2: Ngày 22&23/01/2022

MULTITHREADED PROCESS

ulimit
Cách tìm ulimit trên Linux
Cách tìm ulimit cho người dùng trên Linux

Lệnh ulimit Linux đặt hoặc hiển thị giới hạn tài nguyên tiến trình người dùng. Thông thường,
các giới hạn được tìm thấy trong tệp /etc/security/limits.conf hoặc systemd.
Hai loại giới hạn

Tất cả các giới hạn Linux đều được chia làm hai loại:
- Soft limit
- Hard limit

Xem các ulimit cho tài khoản người dùng của Linux

Nhập lệnh sau để xem tất cả các soft và hard limit cho người dùng hiện tại:
ulimit -Sa ## Show soft limit ##
ulimit -Ha ## Show hard limit ##

$ ulimit -Sa
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7823
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 7823
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

$ ulimit -Ha
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7823
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 65536
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 7823
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

Dynamic memory:

Dynamic memory allocation in C - malloc calloc realloc free

C Dynamic Memory Debugging with Valgrind

malloc() function
Description
The C library function void *malloc(size_t size) allocates the requested memory and returns a
pointer to it.
Declaration
Following is the declaration for malloc() function.
void *malloc(size_t size)
Parameters
● size − This is the size of the memory block, in bytes.
Return Value
This function returns a pointer to the allocated memory, or NULL if the request fails.
Example
The following example shows the usage of malloc() function.
Live Demo
#include <stdio.h>
#include <stdlib.h>
int main () {
char *str;

/* Initial memory allocation */


str = (char *) malloc(15);
strcpy(str, "tutorialspoint");
printf("String = %s, Address = %u\n", str, str);

/* Reallocating memory */
str = (char *) realloc(str, 25);
strcat(str, ".com");
printf("String = %s, Address = %u\n", str, str);

free(str);

return(0);
}
Let us compile and run the above program that will produce the following result −
String = tutorialspoint, Address = 355090448
String = tutorialspoint.com, Address = 355090448
Pthread:
- Tạo file source code taothread.c
- Compile:
$ gcc taothread.c -o taothread -lpthread

POSIX Threads Programming

- POSIX Threads Programming, https://hpc-tutorials.llnl.gov/posix/


-
- https://hpc-tutorials.llnl.gov/posix/mutex_variables/
- https://randu.org/tutorials/threads/

Tạo code: taothread2.c


Compile:
$ gcc taothread2.c -o taothread2 -lpthread

Kết quả:
Tạo, kết thúc, và thiết lập các thuộc tính của Thread:
- https://hpc-tutorials.llnl.gov/posix/creating_and_terminating/
Introduction to Thread: pthreads #1: Introduction
Mutex Synchronization in Linux with Pthreads

Mutex Synchronization in Linux with Pthreads

Viết chương trình tính tổng của mảng dùng Multithread và Mutex:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define NTHREADS 4
#define ARRAYSIZE 1000000
#define ITERATIONS ARRAYSIZE / NTHREADS

double sum=0.0, a[ARRAYSIZE];


pthread_mutex_t sum_mutex;

void *do_work(void *tid)


{
int i, start, *mytid, end;
double mysum=0.0;

/* Initialize my part of the global array and keep local sum */


mytid = (int *) tid;
start = (*mytid * ITERATIONS);
end = start + ITERATIONS;
printf ("Thread %d doing iterations %d to %d\n",*mytid,start,end-1);
for (i=start; i < end ; i++) {
a[i] = i * 1.0;
mysum = mysum + a[i];
}

/* Lock the mutex and update the global sum, then exit */
pthread_mutex_lock (&sum_mutex);
sum = sum + mysum;
pthread_mutex_unlock (&sum_mutex);
pthread_exit(NULL);
}

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


{
int i, start, tids[NTHREADS];
pthread_t threads[NTHREADS];
pthread_attr_t attr;

/* Pthreads setup: initialize mutex and explicitly create threads in a


joinable state (for portability). Pass each thread its loop offset */
pthread_mutex_init(&sum_mutex, NULL);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for (i=0; i<NTHREADS; i++) {
tids[i] = i;
pthread_create(&threads[i], &attr, do_work, (void *) &tids[i]);
}
/* Wait for all threads to complete then print global sum */
for (i=0; i<NTHREADS; i++) {
pthread_join(threads[i], NULL);
}
printf ("Done. Sum= %e \n", sum);

sum=0.0;
for (i=0;i<ARRAYSIZE;i++){
a[i] = i*1.0;
sum = sum + a[i]; }
printf("Check Sum= %e\n",sum);

/* Clean up and exit */


pthread_attr_destroy(&attr);
pthread_mutex_destroy(&sum_mutex);
pthread_exit (NULL);
}

PI simulation
Estimating Pi using Monte Carlo Simulation

Parallel Programming Standards


- Thread Libraries
- Win32 API
- Posix threads
- Compiler Directives
- OpenMP (Shared memory programming)
- Message Passing Libraries - MPI (Distributed memory programming)

OpenMP Tutorial
- https://engineering.purdue.edu/~smidkiff/ece563/files/ECE563OpenMPTutori
al.pdf
- Shared Memory Parallel Programming in the Multi-Core Era • Desktop and
Laptop – 2, 4, 8 cores and … ? • A single node in distributed memory
clusters – Steele cluster node: 2 8 (16) cores • Shared memory hardware
Accelerators • Cell processors: 1 PPE and 8 SPEs • Nvidia Quadro GPUs:
128 processing units
LAB 3:

SYNCHRONIZATION WITH MUTEX, SEMAPHORE

- Submission lab 3:
http://e-learning.hcmut.edu.vn/mod/assign/view.php?id=795031&forceview=
1
- Lập trình với POSIX Pthread: Hàm pthread_mutex_init, pthread_create,
pthread_mutex_lock, pthread_mutex_unlock, pthread_mutex_trylock,...
- Link: https://hpc-tutorials.llnl.gov/posix/example_using_mutexes/
- Mutex Synchronization in Linux with Pthreads:
Mutex Synchronization in Linux with Pthreads

- Producer - Consumer Problem in Multi-Threading


SEMAPHORE PROGRAMMING IN LINUX

- Semaphore for process synchronization:


POSIX Semaphore used with fork()
- Semaphore with Pthread: Introduction to semaphores in C
- Producer - Consumer Problem in Multi-Threading
- Condition variables in C
- Difference between Binary Semaphore vs Mutex:
Difference between Binary Semaphores and Mutexes

You might also like