UNIX
History
History

Multi user

Old School

UNIX FEATURES
- Multi user
- Preemptive multitasking
- Hierarchical directory structure
- Portability
- Virtual memory
KERNEL / SHELL
Kernel / ShELL

Kernel / Shell

User / Kernel Space
C
void main()
{
printf("Hello world\n");
}
toki (~/unix) > gcc helloworld.c
toki (~/unix) > ./a.out
Hello world
toki (~/unix) > ls -ld a.out
-rwxr-xr-x 1 chris lokku 8515 Dec 12 13:07 a.out*
toki (~/unix) > ldd a.out linux-vdso.so.1 => (0x00007fff585fe000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5905530) /lib64/ld-linux-x86-64.so.2 (0x00007f5905916000)
Hello world 2
void main()
{
write(0, "Hello world\n", sizeof("Hello world\n"));
}
toki (~/unix) > ./helloworld2 Hello world
fork / exec
- It's important
- Also, init
FORK()
void main()
{
int ret;b
ret = fork();
printf("Hello world (pid %d). ret = %d\n", getpid(), ret);
}
toki (~/unix) > ./1 Hello world (pid 11450). ret = 11451 Hello world (pid 11451). ret = 0 toki (~/unix) >
File descriptors

Processes


Processes

THREADS

Process MEmory to Kernel Memory


BINARY
BINARY
C
Characters

CODE!
struct listing {
char teaser[10];
int rooms;
int bedrooms;
};
void main()
{
int i;
struct listing li;
memset(&li, 0, sizeof(li));
strcpy(li.teaser, "ateaser");
li.rooms = 5;
li.bedrooms = 3;
char *pt = (char *)&li;
for (i = 0 ; i < sizeof(struct listing) ; i++) {
printf("%c (%hhu)\n", *pt, *pt);
pt++;
}
}
OUTPUT
a (97) t (116) e (101) a (97) s (115) e (101) r (114) (0) (0) (0) (0) (0) (5) (0) (0) (0) (3) (0) (0) (0)
UNIX FILESYSTEM

STAT

Directory system

man fstat
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
man readdir
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file */
char d_name[256]; /* filename */
};
Filesystem Layout

UNIX
By chris_lokku
UNIX
- 835