Unix system calls (1/2)

  Рет қаралды 422,250

Brian Will

Brian Will

13 жыл бұрын

Part of a larger series teaching programming. See codeschool.org

Пікірлер: 188
@R3negade638
@R3negade638 3 жыл бұрын
9 years later and this is the best resource on the topic. Really great.
@DragisaBoca
@DragisaBoca 10 жыл бұрын
Very well made, you made the world a better place.
@BlackM3sh
@BlackM3sh 6 жыл бұрын
At 31:50 you talk about environment variables. However there are some mistakes worth correcting for future viewers. First, although the environment variables are stored in the process' memory, it is stored as zero-terminated strings and not as one big string separated by new-line characters. It is also is not stored on the heap, nor is there a global variable in the data section pointing to it. The environment is actually stored entirely on the stack and is a part of the initial process stack that is set up before the program starts running. The first value on the stack is the argument count followed by an array of the addresses of the different arguments, then address 0 marking the end of the argument array. Right after that there is a second array of addresses which each point to a zero-terminated string which would be the environment variables, this array is also terminated by having address 0 at the end. There is actually a third array of auxiliary vectors but after that there is an unspecified amount of bytes before the information block starts. It's generally inside this block the command line arguments and environment variables are stored, as in the actual string values. You can confirm this by dumping the stack of pretty much any program and you typically find all the environment variables at the very end (highest memory address). If you are on Linux you can do this by first reading the '/proc//maps' file for any process, just replace with that process' PID. This file contains the ranges of memory mapped to the process and what they are mapped to. Near the bottom you'll see one line with the range mapped to [stack]. Take note of the start address and calculate how big it is in bytes. Then run 'sudo xxd -s -l /dev//mem', example 'sudo xxd -s 0x7fff182bd000 -l 0x22000 /dev/14950/mem'. And the environment variables should get printed out together with their hex values and address location. To illustrate this further I've written a small c program that prints all the environment variables using the argv array pointer. As you can see the environment variable pointers are stored pretty much right after argv. #include int main(int argc, char **argv) { for (int i = argc + 2; argv[i] != NULL; i++) { printf("%s ", argv[i]); } return 0; } You can of course make it less stupid by using the full version of main which includes a pointer to the first element in the environment pointer array. #include int main(int argc, char **argv, char **envp) { for (int i = 0; envp[i] != NULL; i++) { printf("%s ", envp[i]); } return 0; } This is all defined as a part of the ABI (application binary interface) for both the x86 and x86_64 architecture, so 32 and 64 bit desktop computers. tl;dr: The environment is not a single long string separated by new-line characters. The environment variables and the pointers to them are both stored on the stack or just before it.
@TheNullBox
@TheNullBox 5 жыл бұрын
Wow man! Thank you :) Both the video and your comment. Amazing stuff!
@payloadartist
@payloadartist 3 жыл бұрын
Thanks. This comment should be pinned!
@crptc5707
@crptc5707 3 жыл бұрын
Thank you sir just tried your code in online c ide and it runs exactly as you described, but other than than video is great!
@sangramjitchakraborty7845
@sangramjitchakraborty7845 3 жыл бұрын
Incredibly informative. Thank you for commenting this.
@leaharrington4472
@leaharrington4472 3 жыл бұрын
While not part of the kernel ABI, as you point out, glibc provides a global variable (char ** environ) pointing to the environment, and may relocate the environment to the heap in setenv() as needed.
@sodapopinski9922
@sodapopinski9922 4 жыл бұрын
so many levels of abstraction,by the time people are clicking on their GUI's it is a symphony of perfect timed and executed processes, but listening to this I can really imagine year after year problems develop and more complicated solutions come into play stepping up a level of abstraction, I mean 60 years is so impressive to see how for we come, from logic gates, MOSFETS to EEPROMS to insane clock speeds to RISC to now but it all started at a level of someone feeding an electronic impulse into a JK FLIP FLOP and trapping that high or low impulse.... it is truly baffling!!!!
@automatenmark5051
@automatenmark5051 Ай бұрын
it's mind blowing but also magical to see how all comes down to some tiny switches
@eshgholah
@eshgholah 10 жыл бұрын
I have loved every second of your videos. Specially the Unix system call series. Could you please kindly do an advanced series of Linux Internals. I know it is too much to ask but obviously you are the right person to do it. I have never seen anyone else describing things so clear and nicely. Thanks a million.
@leonbishop7404
@leonbishop7404 3 жыл бұрын
2:00 kinda sus abbreviation you got there
@Synchr0nix
@Synchr0nix 6 жыл бұрын
I've been watching this video series every day, for the last 3 days, and I learn a little more from it each time, lol. Thanks man. This is one of the most professional lessons I've ever found on KZbin. I can tell you know what you're talking about.
@rzathamesmer
@rzathamesmer 9 жыл бұрын
More Unix videos please! You're an excellent teacher, and the slides are very well done. :))))))))
@ben2258
@ben2258 3 жыл бұрын
I just discovered your channel and can't stop watching your videos! They're incredibly helpful and clear. Just wanted to say it seems to me this and the next video should be added to your Operating Systems playlist.
@pouryamehdinejad8124
@pouryamehdinejad8124 4 жыл бұрын
The best explanation of system calls I could find on the internet. Thanks to Brian Will
@kshahkshah
@kshahkshah 8 жыл бұрын
This is a truly excellent, informative and well laid out video. Thank you so much. I've been coding for 15+ years and got a lot out of this, especially having mostly dealt in interpreted dynamic languages and not having to ever manage memory myself
@mbigras
@mbigras 5 жыл бұрын
I’ve searched for a video like yours for a long time, thank you so much for this work!
@penisafotza4807
@penisafotza4807 3 жыл бұрын
1:46 Oh God! I cant get away from it...
@crptc5707
@crptc5707 3 жыл бұрын
It's a fantastic tutorial! I've been baffled with kernel space and user space for quite a long time and misunderstood that system call incurs context switch between user process and kernel process, until I watched this video... million thanks!
@nikkehtine
@nikkehtine 2 жыл бұрын
High quality presentation and commentary. We get such a long, interesting and informative video for the great low price of free. Thank you so much.
@greymind0072
@greymind0072 3 жыл бұрын
Thanks for making this gem of a video. Your content is lucid and enriching at the same time
@AdamOutler
@AdamOutler 8 жыл бұрын
This is a very informative Linux/Unix System Calls series.
@JoePhilipps
@JoePhilipps 8 жыл бұрын
Applications are ever more security aware, and one caveat with malloc(3) and mmap(2) is to determine what is sensitive in what has been allocated (e.g., storage for passphrases, cached encryption keys, etc.), and that should be zeroed before calls to free(3) or munmap(2). There may be no guarantee by the OS that newly malloc'ed or mmapped regions have been thus scrubbed, so it's up to the process, as best as it can, to sanitize such regions before handing them back.
@AdamOutler
@AdamOutler 8 жыл бұрын
*****​ seems like there should be a system call to handle sensitive data. I wonder if it would be possible to somehow fill the memory with memory requests to just scan for random strings. In what context, though, do you mean apps are more security aware? Are you speaking of just this?
@JoePhilipps
@JoePhilipps 8 жыл бұрын
One should always consult the manual page for the system call you want to use, and about the system for which you wish to program. This guy obviously had to remain generic to cover SysV, Linux, *BSD, OS X, etc., but each system can have other restrictions or features. For example, if you want to write a utility for an SELinux system, you will have contexts to deal with, and such things operate additionally in system calls (e.g., I think child processes after a fork(2) also inherit SELinux contexts).
@JoePhilipps
@JoePhilipps 8 жыл бұрын
_I wonder if it would be possible to somehow fill the memory with memory requests to just scan for random strings. In what context, though, do you mean apps are more security aware?_ Exactly that, Adam Outler . I just wanted people to start thinking more securely if they want to program at the system call level. It's worth a look at a particular OS's manpages or equivalent to see if such conditions are specified, such as [s]brk(2) (upon which malloc(3) is based) zeroing memory pages before they're returned to the process. In fact, because a process can be killed at any time, it is wise in more security minded apps to scrub storage (whether variables/RAM or parts of files) as soon as they're not needed. To a certain extent, you can control this by locking the pages into RAM if you have enough priviledge to the process, so that such RAM will never be written to the swap partition. That represents another potential security threat, the superuser (or anyone with enough access to the underlying device node) sifting through the swap space for such nuggets.
@tigeruppercut7
@tigeruppercut7 9 жыл бұрын
One of the most solid videos I've seen on Linux. Great job. Thanks.
@WeightlessFlex
@WeightlessFlex 3 жыл бұрын
Using this to study up for my interview as a production engineer. Best videos resource I’ve found besides certain books. Thank you. Maybe can you come up with a practice problem series?
@fouzaialaa7962
@fouzaialaa7962 3 жыл бұрын
i studied this in my engineering class and it took them almost 4 months to teach us this ...this 45 minute lecture made it so easy and simple !!! in uni they stretched it so much that you forget about it start questioning everything again every lecture thx for the upload
@arrikd8358
@arrikd8358 3 жыл бұрын
what Uni?
@fouzaialaa7962
@fouzaialaa7962 3 жыл бұрын
@@arrikd8358 ESPRIT in tunisia
@Occcc12
@Occcc12 7 жыл бұрын
What an excellent and clear explanation. thanks a lot for the upload!
@stefanvoicu6484
@stefanvoicu6484 2 жыл бұрын
I have a class on Operating Systems and this has been very helpful! thank you
@JethroYSCao
@JethroYSCao 4 жыл бұрын
When I first learned about permissions on directories, it was said that 'x' allows one to cd into it, even if that might not be the most precise explanation, I think it's a good enough proxy to give users the intuition.
@amarnathp4560
@amarnathp4560 3 жыл бұрын
I am seeing this after 9 years. Nice content
@Yazan_Majdalawi
@Yazan_Majdalawi Жыл бұрын
Wow, where have you been, this is a treasure!
@00chiuppi
@00chiuppi 11 жыл бұрын
BIll - great work here. very helpful for me in understanding issues I'm dealing with on some servers at work. thanks
@waiwinglam8541
@waiwinglam8541 7 жыл бұрын
This is a very quality tutorial. Thanks a lot!
@PauloConstantino167
@PauloConstantino167 3 жыл бұрын
Your content is Gold. Sad to see you inactive........
@TheDerHeld
@TheDerHeld 6 жыл бұрын
great content with awesome sidenotes to give you the big picture - thank you!
@patrickmullen2914
@patrickmullen2914 Жыл бұрын
Thank you for taking the time to make this video. A thumbs up 👍 I'll be viewing more of your videos 🙂 including part 2 of this one
@briantwill
@briantwill 11 жыл бұрын
I'm no expert issue on this issue, but my investigation at the time concluded that brk/sbrk are actually archaic, as the concept of a data segment barrier is outmoded in paged-memory environments. Yes, mapping /dev/mem is not the way to go, but mmap in modern Unixes can do 'annonymous mapping,' which maps to swap-backed memory pages rather than any file. The Wikipedia entry on mmap mentions this. I believe this is what most allocation routines use today, not brk/sbrk.
@Jonathan-od5xc
@Jonathan-od5xc 4 жыл бұрын
This is incredible, thank you.
@ravisaraswat2452
@ravisaraswat2452 6 жыл бұрын
you are the real Guru, thanks a lot , really appreciate your help and videos. :)
@cyrilemeka6987
@cyrilemeka6987 3 ай бұрын
Very informative. I needed this to better understand low level details for the program I am currently writing in C++ and llvm. Thanks
@fuanka1724
@fuanka1724 6 жыл бұрын
Excellent explanation, thank you!
@manojambakkat
@manojambakkat 12 жыл бұрын
Thanks a ton. This is very nice video on system calls and process address space.
@jjpcondor
@jjpcondor 11 жыл бұрын
Fine job, Brian!
@valdasadomaitis719
@valdasadomaitis719 9 жыл бұрын
Came here through codeschool.org while googling for a system call and i'm really enjoying the rest of the content.
@GideonMaina
@GideonMaina 3 жыл бұрын
Great content, thanks for sharing the knowledge.
@lanhsunsiingh4898
@lanhsunsiingh4898 5 жыл бұрын
Great Work Brian!
@aborkar
@aborkar 4 жыл бұрын
Sir, you may be the reason I get my dream job
@briantwill
@briantwill 11 жыл бұрын
Yeah, I should have phrased this better. To my understanding, user groups were created solely with actual groupings of humans in mind, a use case of diminished importance today in most settings.
@shaunmorgan2202
@shaunmorgan2202 6 жыл бұрын
Good video, just a small point. Linux is a kernel, Debian and others are the Unix derivatives that use the Linux kernel.
@landro3552
@landro3552 4 жыл бұрын
0:00 ~ UNIX-like systems 1:46 ~ UNIX standards 2:57 ~ System calls 5:21 ~ Process states
@bob-ny6kn
@bob-ny6kn 3 жыл бұрын
The depth of the information you cover is what scared me away from Computer Science. I call myself a programmer, but obviously I am more a Code Groupie. Your video is very nice, and still relevant so many years after it was made. I wonder if ever architecture will significantly change?
@microto
@microto 8 жыл бұрын
awesome! keep up the good work
@0xrgg965
@0xrgg965 5 жыл бұрын
laughed at "we have a process that is forking itself"
@srenh-p3798
@srenh-p3798 9 ай бұрын
Great video Brian
@yurigansmith
@yurigansmith 10 ай бұрын
Thanks for this insightful playlist. Btw: Can you recommend a good book (or lecture notes) on this topic?
@XavierMJames
@XavierMJames 4 жыл бұрын
Wow ! that's one helpful easy to understand lecture on KZbin
@rajusakthitube
@rajusakthitube 6 жыл бұрын
Thank you so much Brain. very useful video.
@rangapavankurapati2557
@rangapavankurapati2557 2 жыл бұрын
Thank you Sir. Great explained.
@ChandraSekhar-ur1so
@ChandraSekhar-ur1so 8 жыл бұрын
Thanks a lot for the video.
@gavalinilesh80186
@gavalinilesh80186 10 жыл бұрын
Thank you Brian.
@vishals9353
@vishals9353 5 жыл бұрын
It is an excellent video which covers hell lot of things with great clarity in a short time.Thanks for the tutorial.
@DaLakersFan24
@DaLakersFan24 5 жыл бұрын
GREAT VIDEO, THANKS FOR MAKING THIS
@Larock-wu1uu
@Larock-wu1uu 2 жыл бұрын
This is amazing!
@Stakkato98
@Stakkato98 11 жыл бұрын
good work, very straight forward.
@chandanrock4802
@chandanrock4802 4 жыл бұрын
Hello Sir, thank you for your knowledge, all the videos are really great and understandable, Sir I wanted to know name of the book from where I can get this information, thank you
@kami-brawlstars9635
@kami-brawlstars9635 3 жыл бұрын
System call: Generate luminous element! Discharge!
@zaggernut5054
@zaggernut5054 3 жыл бұрын
i gEt tHaT RefeREnCe
@sangramjitchakraborty7845
@sangramjitchakraborty7845 3 жыл бұрын
While watching SAO i kept thinking how insecure the system was.
@jonasfelix7700
@jonasfelix7700 4 жыл бұрын
How does this compare to modern Windows Systems? Would be interesting to see a comparison video.
@aquapurity
@aquapurity 7 жыл бұрын
An amazingly helpful video on the subject. Thank you very much.
@MichaelDCBowen
@MichaelDCBowen 12 жыл бұрын
what is your presentation app? these slides are perfect.
@MaxCoplan
@MaxCoplan 5 жыл бұрын
It says this is one part of a larger series. What is the larger series?
@EmilFihlman
@EmilFihlman 8 жыл бұрын
Great video!
@krux02
@krux02 7 жыл бұрын
I just watched it and I have to say I really liked the information it had. But you should know that in the area of Super Computers, you still have a lot of users logging in to the same system. And super computers are mostly run by unix system and are not likely to die out in the forseeable future.
@shashank88
@shashank88 9 жыл бұрын
Thank you ! crisp and to the point!
@Crux161
@Crux161 12 жыл бұрын
this is great, and i'm all for the smaller chunks however, I feel this video has some volume normalization issues.. That could be something to double check before creating your final version. :D Still Very Awesome
@mohammadalhyari4272
@mohammadalhyari4272 4 жыл бұрын
seems the site is down ?
@samarthtandale9121
@samarthtandale9121 Жыл бұрын
This is a superb playlist! Though this is 11 yrs old, can you please tell where can I find the subsequent videos cauz the link provided in description is out-dated i think ... Please tell where can I get the subsequent videos of this series or upload them in the same playlist on youtube. This is a very kind request of mine ... Thank You though for whatever you have put on the channel for free !!!
@jeffcauhape8110
@jeffcauhape8110 6 жыл бұрын
Well done!
@emnabenayed9995
@emnabenayed9995 10 жыл бұрын
Thank you!
@uboxer
@uboxer 11 жыл бұрын
excellent job, thanks.
@sergioropo3019
@sergioropo3019 5 жыл бұрын
Fantastic!
@m3hdim3hdi
@m3hdim3hdi 4 жыл бұрын
Thank you so much you helped me a lot
@mehdikerdoud6139
@mehdikerdoud6139 Жыл бұрын
thank you so much for thee great explanation
@pauljtomas
@pauljtomas 6 жыл бұрын
Great stuff - thank you
@-XArchLinuxEnjoyerX-
@-XArchLinuxEnjoyerX- 5 жыл бұрын
Where is the larger series? I'd like to watch it! Thanks
@Crux161
@Crux161 11 жыл бұрын
However, I should say that the captions seem to work exceptionally well. :D
@ko95
@ko95 3 жыл бұрын
So a system call is some service that the operating system makes available from hardware and application/processes use these services to function...?
@GrubenM
@GrubenM 6 жыл бұрын
Yep, this is excellent
@Feninou
@Feninou 4 жыл бұрын
Good job man !
@yesterdaysguy
@yesterdaysguy 11 жыл бұрын
Actually it's Berkeley SOFTWARE Distribution. -Nice first pic though gotta love Jurassic Park.
@ooo000ps8
@ooo000ps8 4 жыл бұрын
great video, Thank you very much
@NeelSandellISAWESOME
@NeelSandellISAWESOME 2 жыл бұрын
BSD actually stands for Berkley Software Distribution
@ranjirhodes
@ranjirhodes 3 жыл бұрын
Excellent info , though i had to reduce to 0.75x speed . Thank you for the explanation 👍 can u give info on relation between smaps and mmap and about virtual memory and resident set size
@cindycindy5980
@cindycindy5980 2 жыл бұрын
how does the process split memory into pages? at 19:00 if we can get away with using 8000 bytes of memory when allocating 5000, does this mean a memory error (17:00) only triggers by the page, unless otherwise specified, eg. when a second process allocated the other 3000 bytes?
@briantwill
@briantwill 11 жыл бұрын
Thanks, this is good to know. Still think it's a bit too in depth at this point. I'm already glossing over a lot here, though I don't think I say anything out-and-out false. Do you think there's something misleading?
@aborkar
@aborkar 4 жыл бұрын
No such thing as too in depth mate. Share as much as you know!
@jamebozo
@jamebozo 12 жыл бұрын
good lecture, thank you very much :)
@goldibollocks
@goldibollocks 2 жыл бұрын
The init process be like "I just wanna fork"
@slogslogger8921
@slogslogger8921 9 жыл бұрын
Love it
@petevenuti7355
@petevenuti7355 2 жыл бұрын
I've heard of 'single system image' Linux versions, how does that work? How many of them are there?
@WilcoVerhoef
@WilcoVerhoef 2 жыл бұрын
30:44 the "wait" system call. What if the child process ends very quickly, and another unrelated process starts using the same PID. Will this cause this "wait" call to fail? Is there a way to prevent this with certainty?
@confuded
@confuded 8 жыл бұрын
When he says at 3:59 that the kernel code and table for systems calls is placed at the beginning of each process stack - that means all the system calls are copied again for each process to its memory space?! (yes I know it isn't accessible to the program directly) If this is true, that is a lot of wasted space! Please confirm.
@briantwill
@briantwill 8 жыл бұрын
+Hakkensha Shinsekait (confuded) Each process has this portion of their address space mapped to the same read-only pages of actual RAM, so there is only ever one copy in the system, thus not wasting RAM.
@confuded
@confuded 8 жыл бұрын
+Brian Will So the beginning of the stack labeled "kernel code" simply contains pointers (i.e. is mapped) to actual kernel code (i.e. the read only pages of that code)? I am attempting to reiterate what you said in my own words so you can confirm I got that right =). Thanks for the series by the way! I especially love the OS ones as I could not find good videos on how an OS actually worked practically!
@briantwill
@briantwill 8 жыл бұрын
+Hakkensha Shinsekait (confuded) I wouldn't use the word 'pointer' because that suggests an address in memory pointing to some other address in memory. But yes, the OS can map any virtual page to any physical page, and multiple virtual pages within a process or across processes can all be mapped to the same physical page. In this case, the virtual pages will be marked read-only to prevent a process from mucking with OS code. Any attempt by the process to write to these pages will trigger a CPU exception.
@narayanbhat3279
@narayanbhat3279 3 жыл бұрын
where can i get this course on pluralsight?
@alexanderher7692
@alexanderher7692 7 жыл бұрын
I swear, Im gonna ace this course (OS) without attending it:D
@tochukwunwoko5632
@tochukwunwoko5632 4 жыл бұрын
Great Video
@Alex2Buzz
@Alex2Buzz 6 жыл бұрын
Technically, mmap maps memory at a specified address, while malloc finds free address space and allocates there. Of course, you know this, but I'll just note the correct usage in the comments.
@crateim
@crateim 6 жыл бұрын
mmap is how a userspace program requests memory from the kernel (with an anonymous mapping, and it can, but does not have to specify the address it gets mapped at, it can leave it up to the kernel to choose). `malloc` is not a system call, but a libc function that will use `mmap` under the hood.
@Alex2Buzz
@Alex2Buzz 6 жыл бұрын
Aaron Miller malloc can be implemented using an anonymous mmap, but it can also be implemented with sbrk. I do see your point, but simplifying mmap as just “the memory allocator” is a bit of a misrepresentation.
@GarrickHe
@GarrickHe 6 жыл бұрын
I thought malloc uses sbrk() or was is mmap a recent change? Thanks.
@crateim
@crateim 6 жыл бұрын
Garrick He depends on your libc/malloc, recent ones mostly mmap I believe, but when in doubt strace and find out :)
@BlackJar72
@BlackJar72 6 жыл бұрын
Interesting to note: I have found the trying to access a deallocated C++ object crashes with a seg-fault, but accessing a deallocated C-string with C code results in code that runs with no complaints. (Why I did? Well, it was a challenge experiment, not something I'd ever do in a real program.)
@briantwill
@briantwill 6 жыл бұрын
I assume this C string was a literal in code, right? String-literal strings get put in a permanent memory area that exists at program start and never goes away. I'm curious what platform you did this on? I believe the behavior of free'ing addresses that aren't valid alloc addresses is undefined. Perhaps on your platform invalid free calls just fail silently.
@subee128
@subee128 3 ай бұрын
Thank you very much
Unix system calls (2/2)
51:18
Brian Will
Рет қаралды 102 М.
Operating System Basics
23:16
Brian Will
Рет қаралды 648 М.
Маленькая и средняя фанта
00:56
Multi DO Smile Russian
Рет қаралды 2,9 МЛН
😱СНЯЛ СУПЕР КОТА НА КАМЕРУ⁉
00:37
OMG DEN
Рет қаралды 1,8 МЛН
Каха с волосами
01:00
К-Media
Рет қаралды 6 МЛН
Surprise Gifts #couplegoals
00:21
Jay & Sharon
Рет қаралды 30 МЛН
Comparing C to machine language
10:02
Ben Eater
Рет қаралды 5 МЛН
AT&T Archives: The UNIX Operating System
27:27
AT&T Tech Channel
Рет қаралды 1,9 МЛН
How Do Linux Kernel Drivers Work? - Learning Resource
17:02
LiveOverflow
Рет қаралды 526 М.
everything is open source if you can reverse engineer (try it RIGHT NOW!)
13:56
Low Level Learning
Рет қаралды 1,2 МЛН
SysVinit vs Systemd
31:19
DJ Ware
Рет қаралды 51 М.
Unix vs Linux
13:59
Gary Explains
Рет қаралды 1,6 МЛН
Using Strace to Trace Linux Syscalls
16:20
LaurieWired
Рет қаралды 12 М.
What is a kernel - Gary explains
9:50
Android Authority
Рет қаралды 947 М.
Маленькая и средняя фанта
00:56
Multi DO Smile Russian
Рет қаралды 2,9 МЛН