0% found this document useful (0 votes)
64 views

Linux Lecture

This document provides information on Linux distributions, kernel architecture, source code browsing tools, static code analysis, system calls tracing, patching source code, process execution, and the exec family of functions. It describes how Linux distribution versions are numbered based on the major and minor numbers. The kernel architecture uses a process space divided between user mode and kernel mode. Tools like ctags, cscope, and exuberant-ctags are recommended for browsing kernel source code. Static analysis tools like splint can check for bugs and standards compliance. System calls can be traced using strace and ltrace to debug programs. The exec family of functions like execv and execvp are used to execute new processes in place of the calling process

Uploaded by

Abhinav Anand
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Linux Lecture

This document provides information on Linux distributions, kernel architecture, source code browsing tools, static code analysis, system calls tracing, patching source code, process execution, and the exec family of functions. It describes how Linux distribution versions are numbered based on the major and minor numbers. The kernel architecture uses a process space divided between user mode and kernel mode. Tools like ctags, cscope, and exuberant-ctags are recommended for browsing kernel source code. Static analysis tools like splint can check for bugs and standards compliance. System calls can be traced using strace and ltrace to debug programs. The exec family of functions like execv and execvp are used to execute new processes in place of the calling process

Uploaded by

Abhinav Anand
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

19.08.

2010

Distribution Number

1. Uname -a
2. Cat /etc/issue
3. 2.x.y
4. 2 is the major number
5. X is the minor number
6. Y is the patch label
7. If x is even then stable release
8. If x is odd then beta release
9. The minor number is almost over as 2.7 has not been used

Linus Torvalds

Minix : Andrew Tattenbaum

Architecture Library

1. Kernel
User Mode | sytem call
2. User mode , glibc has printf module
3. System call
4. Process space Kernel
5. Kernel mode completely priviledged
6. In microkernel, we use message passing architecture Device Drivers
7. In linux kernel ,its self help, process context changes and executes in kernel mode.
8. Printf is a library call , it becomes a system call (write) which performs in kernel mode.
9. API set – is a mix of library + system calls

Source Code Browsing


1. Grep & find
2. Xargs – extract argument
3. 2>dev/null – send error away
4. –color=auto – color code the search
5. –Hn c1 – one line above , one line above.
6. Cd <kernel-souce-tree>
7. Find . –name ‘*.chsS’ –
8. Refine the search.
9. Superior Tools for Browsing:
a. Exuberant  Ctags:
b. Most text editor support ctags.
c. Supports 30 programming languages
d. Build the ctags file
e. Ctags –R
f. Rebuild after changes to source code
g. Ctags –exclude [useless branches] –R
h. Exclude = ‘@filename’
i. Ls –lh tags : big file
j. Search in :vi:
k. Go to kernel directory where tags is stored
l. Vim kernel/sched.c
m. Ctrl^] to seach for the definition
n. Ctrl + T back to last position.
o. Multiple entries :tsel or :tjump – shows all the definition figure out the correct one and press the
number
p. Maintains a tag stack.
q. To search for anything : Open any file and type
i. :TAG MAX_ORDER
r. CSCOPE
i. Install cscope , Build cscope index
ii. Follow the menu for source code browsing
iii. Cscope –b –q –R : -q reverse index for faster browsing
iv. Ls –lh cscope .out
s. Make help in linux
t. Make ctags inside kernel souce tree
u. Export EDITOR = vim [for code highlighting and etc] by default “vi”
v. Terminal based graphics
w. Very powerful as u can build the call tree.
x. Ctag inside the file … from outside cscope
y. Ctag + cscope : very useful
z. Regex search supported in cscope
aa. Ctrl+D to come out

STATIC code analysis with splint

1. Lint from MIT


a. Lint for linux
b. Secure Programming Lint(SPlINT) at Virginia
c. Download & install
d. Gvim 
e. Memory leak … free after use
f. Splint leaky.c
g. Turn off/on specific features.
h. Follow POSIX standard or any other standard
i. For malloc .. include<stdlib.h> malloc(size_t)
j. Return of fflush important to check .
k. Usleep – micro sleep .. usleep(k) – check failure cases - read man pages
l. Annotate the source code
m. Run with –weak to reduce false positives
2. Strace (http://en.wikipedia.org/wiki/Strace)
a. for tracing system calls
b. Initial starts are mainly non-useful
c. Touch updates the time and date of modification of a file
d. Exit(0) = exit_group(0)
e. Useful for troubleshoot
f. Strace output is send to standard error therefore redirect using 2>ff.out
g. Profile of what is goin on under neath …
3. Syslog
a. Pgrep rsyslogd
4. Ltrace
a. For tracing library calls.
b. Useful for programs that use library calls
c. Eg: xwindows sytem,
d. Neither ltrace and strace cannot show inside the kernel
e. For inside kernel , we have other tools , opensource tools
f. Ltrace and strace leverage ptrace system call to attach to other programs
5. Patching
a. Linux open source world
b. Download and install, study
c. Bug or enhancement
d. Go ahead and do it
e. Patch = diff between original and your version
f. Works on binary files
g. We talk for source codes
h. Diff between files and folders[ take recursive diff]
i. Redirect to a path file …. Delta of the two files
j. Email the patch and they will apply it to the their source base
k. Source code of 2.6.31
l. Download the source code of 2.6.32 or just download a patch …
m. Fetch the patch and apply to your source code and enhance
n. Diff –u : unified diff
o. Apply the patch using patch utility
p. Patch –dry-run : applies only in the Ram : dummy run to check if everything is ok
q. Utility called “meld” which does the patch : very useful for development: can also do it recursively
r. Before “commit” , u should do it in this manner
s. Winmerge for windows : very useful tool on windows

t. Dome when doing codereview :

Homework:

1. Download a kernel and apply the patch and view the changes
2. Highly useful

LINUX SYSTEM PROGRAMMING


1. Every process has a pid, uid and gid. [ process,user, group id for the process]
2. When u login ur uid and gid is stored in /etc/passwd
3. Any process run from the login shell , inherit the numbers eg: ‘vi’
4. Uid is split into 2 values RUID[real] AND EUID[effective] maintained by kernel[context
information of the process] [ called process credentials] [ not in user space ]
5. GID is also split in RGID and EGID.
6. Kernel does the check for EUID
7. SUID – set user id bit is set
8. For a process with SUID set .. the EUID changes and becomes the owner of the
executable file
9. Which passwd ; ls –l pathname
10. SUID very useful technique
11.Similar SGID bit can be set : EGID is set to the owner
12.SUID utility is taken care … then its not very harmful
13.Geteuid
14.Init code runs as the user [0 to t1 ]
15.Euid = 0
16.And after time t1 setuid(getuid());

PROCESS EXECUTION

1. VM is a technique through which every process is made to believe that the entire space
is available to it.
2. Virtual memory fools the processes into believing
3. VM layer of the and MMU of the CPU
4. Mordern PC environment
5. Process image is always used
6. A process is imagined to have a memory from low address to high address
7. Runtime image
8. Uninitialized data segment (bss)
9. Homogeneous segments eg: text , data and stack.
10.Check using ps aux to lok for virtual and physical space used by the process
11.Process P can exec another process
12.When a process exec’s another process, the same process image is over-laid by the new
process. So the new process gets the process id of the predecessor
13.Eg: try exec in a shell … shell closes as it is overlaid by the new process. Exec bash wrks.
14.Err no is the uninitialized of the data segment. Kernel tells by updating the error number
the reason for the error
15.perror() is a useful functions. Do the clean-up
16.exec –family of routines …… to use in ur convenient way
17.
18.

0
text

data

Physical

Memory

stack

High
EXEC FAMILY
1. Argv[0] , argv[1] , …..
2. Execl
a. Pathname,
b. Execl(“/bin/ls”, “ls”, “-l”, “abc”, 0);
3. Execclp
4. Execv
5. Execvp : doesn’t require the pathname.
6. Execle
a. With copy of the environment
b. With the previous 4 the predecessor will inherit the environment of its successor.
c. Envp is a char** …. Callers environment variable changes
7. Execve is the main call …. Busy system call
a. Everything runs execve
b. Strace program call used the execve system call
8. Prove that the exec call overlays the pid of the parent process ….
a. Write 2 c programs
b. Or call ps from the program as ps prints its pid
9. Change the environment variable ….. setenv() and printenv()
a. Make a copy of the environment
b. Strncmp – sophisticated version of strcmp …. Maximum number of characters to
compare
c. Printenv
d. ./envp | grep ‘^TERM’
10.STACK_FRAME_BUFFE
a. […..
b. LOCALS  TOP(ESP); LOWER ADDRESS
c. …/
d. SFP  EBP
e. RET ADDR
f. […..
g. PARAMS  BOTTOM; higher addresses
h. ….]
i. If you give a large input file to program eg: if it has gets() statement.
j. The process will crash .. “gets” is a badly coded library API …. Has no limit on the
number of
k. Buffer overflow exploit : done by the hackers
l. They compile the code of execl(“bin/sh”, …….)
m. Compile and take out the machine code ….. take the hex bytes and place it in a
buffer called the “crafted buffer”
n. They make the return address proint bck to top ….. so when program returns it
finally execs a shell
o. Crafted attack has been goin on for years.
p. Most frequent form of attack on unix
q. Use fgets where the maximum number of bits can be fixed …… use strncmp as the
number of bits … snprintf() …….. practice this as u can block the security
loopholes
r. SECURE PROGRAMMING FOR LINUX/UNIX -- free book
s. REDHAT has stack guard feature remove the byte codes from data portion

FORK :
1. Fork makes a copy of the process … the image is not over laid
2. Parent remains alive and the new child comes and runs in parallel to the parent
3. Multitasking OS using fork().
4. Instruction executed by both parent/ child is the next instruction after fork()
5. Child starts from the same position as parent
6. So for a division of labour,
7. ASS|U|ME – ass out of u and me
8. Fork is called once and returned in 2 processes both parent and child
9. Kernel guarantees different return value allowing the developer to knw who is the
parent and child
10.use switch on n : switch(n){ case -1 : do the clean-up case 0: child default : parent }
11.

p c
12.Data is copied across the fork
13.The order of execution is indeterminate
14.Synchronization can be used
15.Int fd= open(“abc”, 0);
16.User space and kernel space
17.Open file descritptor table .. simplistically arry of slots and pointers to the open file data
structure[ file table entry, fte]
18.Fd = 0,1,2 are reserved for input, output, error
19.For the child process a copy of the parent’s OFDT is given to the child. Open files are
truly shared between the processes forked. The file is opened for the child. This is the
fall out of the unix implementation.
20.The child process has acess to the opened file as child inherits all the permission
21.Simultaneous I/O can corrupt the file . The seek pointer is same for the both parent and
child. No guarantee for atomicity.
22.Close(fd) in one of the process. Just the link goes down.
23.If we need it on the both the process. Do file locking. Crude pseudo file locking using
semaphore.
24.File locking API’s available with unix.
25.Advisory locking is preferred over mandatory locking.
26.Advise others that we are using the file , its upto u to handle the file carefully
27.Getpid(),Getppid() ,return of fork() for child pid
28.Many attributes get auto-inherited by the child eg: pwd, root-directory,
umask,credentials(RUID,… same priveledge level as parent), open files, environment
variables.
29.Pid, ppid,utime, file locks, timer ,alarmsare not inherited
30.All the time counters are reset for the children.

RACE CONDITION : WAITING FOR A CHILD PROCESS


1. Wait() is a perfect example for a blocking call
2. Blocking call puts a caller to sleep mode and the kernel awakens it up as soon as the
condition is met. Eg: sleep(5)
3. Parent waits on the death of the child. Kernel guarantees that it ll awaken it as soon
as the child dies.
4. Example a shell process :
a. Run ls –l
b. Shell forks ….. the child shell exec and commits suicide by exec new ls –l
c. Fork and child exec “ – technique in unix.
d. Parent waits for the child to die and displays the shell promt
5. Exercise : write a simple shell
6. Sleep mean being in the wait queue ….. i.e. it cannot even run during the sleep….
Guaranteed not to run when in sleep.
7. Kernel is in a loop checking which program to wake up
8. Eg: Process P has 2 child process c1, and c2
9. Death of any child unblocks the parent… return value of the wait is the pid of the
child that dies. We can track what is happening with the children
10.Multiple waits can be used to wait for multiple children .
11. If u issue a wait without having a child ….. the kernel chcks whether u have a child
and returns -1 and process resumes. The wait fails.
12.Pid_t wiat(int *status)
13.Pid_t stores whether success or failure.
14.Kernel updates the bit of the integer … telling abt how the child really died
15.We can use bitwise masking to chck which bit is set or not
16.Macros are defined are sys/types.h
17.Passing to the macros is the status value
18.WIFEXITED(status) ….. did the child died
19.WIFSIGNALED – if abnormally killed
20.WTERMSIG – murderer
21.WIFSTOPPED – suspended state wait can be unblocked by stopped….. so easily
fooled if not checked into believing that child died
22.WSTOPSIG –
23.Variations on wait
a. Pid_t Waitpid(pid_t pid,int *status, int options) – on which pid or child
b. Wait3(int *status,int options,struct *rusage);
c. Wait4(pid_t pid , int *status, int options,struct rusage *rusage); - superset of
all . In linux wait4 is the main system calls; all other are the wrappers.
d. If not interested in any parameter nullify it.

FORK OPTIMIZATION :

1. Job of the fork : copy of the parent and child. Fork() is a heavy weight process
2. Text segment of a process is “r_x” … u cannot write on the process.
3. So the fork does not copy text section but shares between all the parent and children
4. Data is “rwx” , but even then it is not copied, handled using a technique called
COW(copy on write)
5. COW operation copies the entire page containing that variable … and that is why
very fast
6. Very efficient and frugal technique to use COW – heavily used in linux
7. BSD system engineer improvement – how the shell works fork() and exec() . After
fork() ,, exec() …. It’s the wastage of the effort done by fork().
8. So, vfork() virtual fork()… still in the same memory as the parent. If the new child
calls execve it gets a new image. Check in the main page. Kernel
9. Vfork has use cases : used in uclinux … fork() is not allowed
10.Vfork() not portable across all linux not POSIX standard . need to take care as a
programmer as
11.Implement a shell mysh.
12.If a parent dies … kernel changes the ppid of the child process to 1 [ init process].
Orphaned process is reparented by the init process.
13.Wait() – maintains synchronization between parent and child
14.In unix design the parent must wiat for the child. If the parent doesn’t do that
15.So if a process dies …. It is called a Zombie process – kernel still maintains that the
process has not died .. but the child has really died
16.Zombie – “ a process which has died but not yet buried”
17.Zombies take kernel memory ,also takes pids, server can get
18.You cannot kill the zombie. So, unix design is too be maintained
19.In classic unix, the zombies die only on reboot
20.In linux, if the parent dies the zombie are killed . IBM AIX also uses similar technique.
21.Write a program for orphaned process and zombie process .
22.Ps –l to check zombie(z) and orphaned(ppid=1).
23.Zombies are also called defunct processes

SIGNALLING WITH POSIX SIGACTION :


1. Designing own signal handler. Bypass the default signal handler
2. POSIX.1 signals …. All Oss which are POSIX complieant support the command
3. Don’t use the number …. Use the hash define as it dependent on implementation
4. SIGKILL, SIGSTOP are AEF and DEF ….. i.e. they cannot be caught nor ignored. If you
define handler for them, you would not be able to get it.
5. #include <signal.h>
6. Int sigaction(int sig, const struct sigaction *act, struct sigaction *oldact));
7. *act – is the new setting
8. *oldact – the previous setting , if you want to save it for future use.
9. Real time enabled then we have 2 more structures
10.Struct sigaction{
11. Void(*sa_handler)(int);
12. ……..
13.……}
14.Signal handlers cannot return value as they are called asynchronously
15.Act.sa_handler = catcher;
16.Static void catcher(int signo)
17.Sigemptyset(@axt.sa_mask)
18.Act.sa_flags=0;
19.Reentrance safe function.
20.Reason for safe is that they work on local data.
21.Takecare while writing a library. Must be reentrance safe.
22.“UNIX SYSTEM PROGRAMMING” robbins and robbins
23.Async-signal safe = reentrance safe
24.Printf,sprint,fprintf etx are not safe.
25.Its better to use WRITE/READ system call.
26.Look in the man pages “man 7 signal”
27.“TESTING CAN ONLY PROVE THE PRESCENCE OF A BUG NOT ABSENCE”
28.SIGCLD , SIGCHLD …. Informs the parent that it child has died
29.A parent is unblocked even when child is stopped
30.Signal are reset to default so u can reset in signal handler function but not useful in real
world as there is microsecond gap and signal can come in between[use SA_ONESHOT]
31.If the signal reoccurs in between the execution. Signal is masked during signal handling.
It is deferred in the duration.
32.Sigaction flags
a. SA_NOCLDSTOP : No child death on stop only send me when it actually dies
b. SA_NOCLDWAIT: Linux 2.6 uses SiGCHLD
c. SA_
d. SA_RESTART : EINTR is used
33.Signal masking can be used to bypass signal during the execution of
34.Per process you have a signal mask attached . By default all the bits are 0
35.#def DEBUG
36.#IFDEFDEBUG for debugging
37.Better to use MSG ……user defined function
38.For each function it comes only once. Only 1 instance of the pending signal is queued …
the remainder are discarded. When u are blocking a signal, you will lose n-1 signals.
39.In most situation this is fine
40.This is because SIGNAL PENDING MASK = is a Boolean which tells whether it has come or
not. Not a counter variable.
41.Chck using ps –la …. The pause state is reflected
42.Ctrl+z – stop and then kill %1 – kill job number 1
43.You can queue some of the signals SIGRT – real time… you have a system global
maximum
44.
23.08.2010
1. S_ISREG … the series of function are cleaner way to test the file modes
2. Man stat 
3. STAT system call shows an entire set of information about file.
4. Ls utility also gets all the information through stat dat structure
5. Time is stored unix date and time format . use routine ctime to convert to the standard
format
6. .(dot) file is an an index of the inode . Linkage between file-name and inodes.
7. Stat system call is the one to use to get any inode details

FORK()

1. Printf(“hello”); fork()
2. Hello is stored in a buffer . Buffering is always done in unix.
3. Fork() gets a copy, so it gets the buffer space.
4. All buffer are flushed in parent and child.
5. Buffer size is around 4096bytes size of a page.
6. Flushing of buffer is done when page is filled or program exit
7. SYN – to synchronize the writes and read
8. Fully buffered 4096
9. Line buffer I/O 128 bytes
10.Character buffer I/O 8 bytes
11.Raw I/O is unbuffered I/O …. Read or write. 8bytes
12.Setvbuf to change the buffer size.
13.Return value of read/write is number of bytes written or read
14.BOF / EOF – begin/end of file
15.LSEEK to set the file-pointer – rewind operation
16.Fread/fwrite/fseek – for higher level abstraction
IPC
1. System Call to create pipes.
2. Pipe is a page of RAM.
3. Visualize it as a pipe….. an abstraction
4. Treated as 2 open files.
5. Access to either end is via a file descriptor
6. A pipe object : read end and a write end.
7. A process can write data to write end and read from the read end
8. Int fd[2] .
9. I/O with pipes …. Use read/write
10.Unidirectional – data can flow from one end to another.
11.Fd[0] – read end ; fd[1] – write end of the file
12.Read(fd[0], buf,n);
13.I/O is blocking in nature. If you a read from an empty pipe , the process waits.
14.Default POSIX standard is blocking…. Which helps in auto-synchronization
15.Write(fd[1],buf,n)
16.Macro – PIPE_MAX …. If the pipe is full the write call will block.
17.File-reads are non-desctructive but pipe read is destructive.
18.EOF returned when read is empty
19.Signal SIGPIPE when write is done with buffer full.
20.2 related processes communication.
21.OFBD is shared between forked process. 2 descriptors of the pipe are created in the
OFDB.
22.The read end and write end can be accessed by both parent & child
23.“Ctrl+ c” kills all the parent and child ….. signal is broadcasted to all the processes in the
foreground
24.Check return values from read/write and use signal handler for pipes.
25.Fdptoc and fdctop : 2 pipes for mutual talks
26.“System” function calls – run all calls in child shell
27.Popen gives the power to read in the standard out of any process and also capture the
standard out of any other process.
28.Popen internally creates pipe and helps us to read/write to the process. Returns a file
pointer.
29.Pclose to finish off.
30.Fp = popen(“date”,”r”)
31.Fgets(p,80,fp);
32.Popen gives an abstraction layer above the system to run all the
33.File pointer to file descriptor – fdopen
34. Popen is heavy-weight

DUP – duplicating file descriptor


1. #include<unistd.h>
2. Int dup(int oldfd)
3. Int dup2(int oldfd, int newfd)
4. Returns Lowest available file descriptor.
5. Dup duplicatres the files at the lowest file descriptor position.
6. > operator works in this manner …. Close(1) ; dup(fd[1]); close(fd[1]);
7. Redirection works in this manner
8. Exec in a process copies the File Descriptor of the parent.
9. In effect an implementation of a pipe.
10.Dup2 : u can give the the new file descriptor needed.
11.Only related processes can communicate.

Named Pipes
1. Man mkfifo – user level command
2. Mkfifo –help
3. Cat > afifo

System V IPC
1. Pass the key value… part of the creating API…. Return is the handle to the IPC object [id]
2. Another process P2 specifies a similar-key value.
3. IPC objects are persistent in memory.
4. If you create an IPC object , can be shared
5. Clean-up needs to done finally .. or the IPC object will remain.
6. API changes with the kind of IPC you are working.
7. Message queue [similar to linked list]
8. Semaphore [used as a mutex]
9. Richard Stevens – network programming
10.Beej guide to UNIX IPC
11.Xxxget to get handle to the API’s – shm,ssh,mss – for creation and access
12.Architecture is typically client server.
13.1st parameter is the key value key_t key.
14.IPC_PRIVATE means to create a new object
15.Ftok() to make new unique id.
16.Xxxctl()
17.Ipcs –l to check the IPC system V configuration

Message queue

1. Ca be figured as a message queue.


2. The first member is always an m_type [message type]
3. The member has to be integer.
4. Msgget(key_t key, int msgflg)
5. Eg. of msgflg- IPC_CREATE | IPC_EXCN - various flags available.
6. IPC_RMID, IPC_STAT, IPC_PRIVATE etc……
7. Message boundaries are preserved …. U can read complete node
8. Write fails when u r waiting to write , if it gets deleted returns EIDRM [ id removed]
9. SA_RESTART should have done the job but doesn’t … so we have to use old unix
boys technique of while loop waiting for success and error of EINTR.
10.You can ask it to go out of order by using msgtype paramenter.
11.Read as non-blocking : IPC_NOWAIT
12.Payload should be defined.
13.Ipcs –q to check the mssg queues. Mssg queues are system wide.
14.%.*s : no idea if null terminated.
15.Ps –l shows where it is blocking
16.Ipcrm q ”id” - manual deletion.
17.Ipcs –q
24.08.2010

SEMAPHORE
1. Semaphore problem with creation and initialization interval.
2. Semctl() is the API for all kind of controls

MULTITHREADING
1. The libraries get memorymapped in the virtual memory segment.
2. Process VM[virtual memory] has text,data ….[libraries] , stack.
3. A process has OFDT, IPC segments, environement variables,signal hanling, the max
limits.
4. All threads live in a process address space.
5. The threads share everthing except the stack. Stack implements function calls … that is
why different stack.
6. We need a separate stack space.
7. Sun’s LWP library.
8. Posix1C = Pthread .. set the standard , not implement .. vendors implement.
9. Thread scheduling can be 2-tier with thread library select the winner thread and then
the kernel scheduler will select the winner thread. Unix does so. This is called m to n
mapping.Solaris 9-10 is the change of 2-tier to single tier
10.Linux does single-tier … one to one mapping.
11.Map a thread to a cpu you want. The technique is available but less used.
12.Pthread mutex for mutual exclusion on the global data.
13.Local data is on the stack .. not shared.
14.Same OFDT shared. So file coupling is tighter.
15.No COW in case of data in the threads.. It’s the same data.
16.SUSV – 1,2
17. Benchmark using the code. Time ./pthread & time ./fork
18.Non-Overlapping between the CPU
19.Master-slave model and can easily set the priority of the threads.
20.Pthread not in the libc library. –l pthread.
21.If you call exit() entire process will die. Call pthread_exit() to kill a specific thread.
22.Returns to the thread that is waiting[pthread_join caller] on this thread.
23.If main() calls exit() all the threads are killed.
24.But if main calls pthread_exit(NULL) … main only dies and the others remain alive.
25.Verify the shared library list using command ldd ./pthread_test. The loader is the /lib/ld-
linux.so. … The routines are brought to virtual space of the process. Eg… ldd /bin/ls ..
ldd is an important utility
26.Which firefox
27.File /usr/bin/firefox
28.Find the binary and run ldd x
29. Getpid equivalent is pthread_self()
30.Boolean equal cannot be used between structs so use pthread_equal(thread1,thread2).
31.Joining of threads …. Closest equivalent to wait() of the parent for the child.
32.U can’t make a detached thread joinable after it is created but the reverse is possible
using Pthread_detach().
33.Thread is joinable but the parent doesn’t call pthread_join so not waiting. The meta-
data of the thread is not freed out when the thread gets killed. Closest situation to a
zombie.
34.Pthread_attr_destroy() to prevent memory leak as the pthread_attr_init() calls malloc.
Very important to read man pages to understand expectations.
35.TSD – Thread safe data.
36.Errno is not thread_safe. –DREENTRANT flag creates private errno per thread.
37.Pass by value is better than pass by reference for thread-unsafe data . For thread-safe
data you can pass the address.
38.Pthread_mutexes :
39.Pthread_mutex_trylock() :similar to NOWAIT version of system V : usueful for busy
waiting, but reduced chances of getting lock.
40.After the unlock , a small delay helps to make the distribution fair and reduces the
starvation.
41.ThumbRule : “lock the data not the code”
42.Locking granurality is important. Follow the middle path.
43.Priority Inversion :
44.Condition Variables : Difference from mutex …. Technique superior to polling
45.CV is always paired with a mutex.
46.Mutex is passed along with the CV.
47.Signals are sent if the variable reaches a particular required value.
48.Pthread_cond_wait() : automatically and atomically puts the thread into sleep and
unlocks the mutex and wakes the thread up only when the other processs signals.
49.“what really happened on mars”
50.PRIORITY_INHERITANCE : to make it the maximum priority thread.
51.

SOCKET PROGRAMMING
1. Different types of socket. A socket fie generated at the time of socket creation.
2. Htons, htonl, ntohs, ntohl [l-32 , s-16 bit] ip address is 32 bits , port is 16bits
3. Listen() has a backlog number …. Length of the queue to accept as the backlog.
4.
5.
Process Scheduling on Linux

1. SETTASK , CHRT utility available. Dwnld & install


2. Change the number of cores in the the linux .

Ctrl+alt+F1 – for console mode … to see the smooth pre-emption.


Cc –g -ggdb –o0 -Wall

1. N means step over – run the function


2. S – means step into a function
3. Implicit declaration – header file is missing 

PROC
1. On RAM … not in the hard disks
2. Always on RAM .. loaded at the time of start-up
3. /proc/sys - files are used to tune the system.
4. Turn on or turn off the features
5. PID of the process appears as a folder
6. READ ABOUT PROC ON LINUX
7. Ls –l fd - gives the file descriptors open : 0,1,2 … stdin,stdout
8. /proc/cpuinfo – cpu info
9. /proc/meminfo – mem info
10.Very powerful
11.Free –m ---- uses proc/meminfo internally
12.Cat partitions
13.Cat /proc/versions
14.Cat threads-max
15.U can go down the thread lists using /proc/pid/task and perform similar options.
16.The usage of /proc in a shell script should be done using check whether /proc is
available on the linux -version
17. Read module 13

You might also like