0S Labs Inclass Guide
0S Labs Inclass Guide
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 …
- 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.
-
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.
Process states
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:
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;
/* 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
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
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
/* 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);
}
sum=0.0;
for (i=0;i<ARRAYSIZE;i++){
a[i] = i*1.0;
sum = sum + a[i]; }
printf("Check Sum= %e\n",sum);
PI simulation
Estimating Pi using Monte Carlo Simulation
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:
- 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