You are on page 1of 49

System Programming (CENG204)

Lecturer: Ahmet ÖZDİL


Course Info
Weekly Topics
1 Overview of systems programming
2 Users, files and manuals
3 Directories, file properties and file systems
4 Terminal control and signals
5 Event driven programming
6 Processes and programs
7 i/o redirection and pipes
8 Mid-term Exam
9 Servers and sockets
10 Threads
11 Memory management system and processes carried out under the operating systems
12 Concurrency control
13 Project/Homework Presentation
14 Project/Homework Presentation
15 Project/Homework Presentation
16 Project/Homework Presentation
2
Users, files and manuals
Users and Groups
In linux users:
• 3 categories: user, group, others
• Superuser: has special privileges (User ID: 0, login name: root)
• Unix is a multi-user, multi-tasking OS.
• Multiple users may run multiple tasks concurrently.

3
Users, files and manuals
Users and Groups
Linux/Unix operating systems have the ability to multitask in a
manner similar to other operating systems.
Linux was designed to allow more than one user to have access
to the system at the same time.
In order for this multiuser design to work properly, there needs
to be a method to protect users from each other.
This is where permissions come in to play.

4
Users, files and manuals
Users and Groups
Read, Write, & Execute Permissions
Permissions are the “rights” to act on a file or directory.
Read: a readable permission allows the contents of the file to be
viewed, on a directory allows you to list the contents of a directory.
Write: a write permission on a file allows you to modify the contents
of that file. For a directory, allows to edit the contents of a directory
(e.g. add/delete files).
Execute: for a file, the executable permission allows you to run the
file and execute a program or script. For a directory, the execute
permission allows you to change to a different directory and make it your
current working directory.
Users usually have a default group, but they may belong to several
additional groups.
5
Users, files and manuals
Users and Groups
Linux Primary Groups
A primary group is the default group that a user account belongs to.
Every user on Linux belongs to a primary group.
A user’s primary group is usually the group that is recorded in your
Linux system’s /etc/passwd file.
You can find a user’s primary group ID by viewing the contents of
the your system’s /etc/passwd file.
$ cat /etc/passwd
The fourth column contains each user’s primary group ID. In the
example output, the primary group ID for example_user is 1001:
postfix:x:106:113::/var/spool/postfix:/usr/sbin/nologin
example_user:x:1000:1001:,,,:/home/example_user:/bin/bash
6
Users, files and manuals
Users and Groups
You can also find a user’s primary group information by using
the "id" command.
$ id example_user
uid=1000(example_user)gid=1001(example_group)groups=1001(exampl
e_group),27(sudo)
$ id -gn example_user
example_group

7
Users, files and manuals
Users and Groups
Linux Secondary Groups
Once a user has been created with their primary group, they can be
added to secondary groups. Linux system users can have a maximum
of 15 secondary groups.
A Linux system’s groups are stored in the /etc/group file.
To find the group(s) a user belongs to, run the following command:
$ groups example_user
The example output displays a user’s primary and secondary group(s):
$ example_user : example_group sudo

8
Users, files and manuals
Users and Groups
To add a user to a secondary group use the example command.
$ sudo usermod -a -G second_example_group example_user
$ sudo usermod -a –G second_example_group, third_example_group,
fourth_example_group example_user
What is the difference between Primary and Secondary groups
in Linux?
Every user must belong to a primary group and a user can only belong
to one primary group. Any new directories or files created by a user are
automatically associated with a user’s primary group.
A secondary group is a group that a user is added to after their user
account is created. A user can belong to zero or more secondary
groups.

9
Users, files and manuals
Users and Groups
Creating and Deleting User Accounts
To create a new standard user, use the useradd command. The syntax
is as follows:
$ useradd <name>
You need to set a password for the new user by using the "passwd" co
mmand. Note, you need root privileges to change a user password. The
syntax is as follows:
$ passwd <username>

10
Users, files and manuals
Users and Groups
Creating and Deleting User Accounts
To remove a user account, enter the following command:
$ userdel <name>
Issuing the command above only deletes the user’s account. Their
files and home directory are not be deleted.
To remove the user, their home folder, and their files, use this
command:
$ userdel -r <name>

11
Users, files and manuals
Users and Groups
Working with Groups
Linux uses groups as a way to organize users.
Groups organize collections of accounts, primarily as a security
measure.
Control of group membership is administered through the /etc/group
file, which shows a list of groups and its members.

12
Users, files and manuals
Users and Groups
Working with Groups
To run programs or create a file in a different group, the user must run
the newgrp command to switch their current group.
$ newgrp <marketing>
If the user entering the above-referenced command is a member of the
marketing group in the /etc/group file, then the current group
membership changes.
It is important to note that any files created are now be associated
with the marketing group rather than the user’s primary group.

13
Users, files and manuals
Files
Changing Directory and File Permissions
To view file permissions and ownership on files and
directories, use the ls -al command. The a option is to show
hidden files or all files, and the l option is for the long listing.
The output is similar to the following:
drwxr-xr-x 2 user user 4096 Jan 9 10:11 documents
-rw-r--r-- 1 user user 675 Jan 7 12:05 .profile
drwxr-xr-x 4 user user 4096 Jan 7 14:55 public

14
Users, files and manuals
Files
Changing Directory and File Permissions
drwxr-xr-x 2 user user 4096 Jan 9 10:11 documents

Details of above line:

`drwxr-xr-x` are the permissions


`2` is the number of files or directories
`user` is the owner
`user` is the group
`4096` is the size
`Jan 9 10:11` is the date/time of last access
`documents` is the directory

15
Users, files and manuals
Files
Changing Directory and File Permissions
Understanding The Permission Syntax
To use chmod to set permissions, we need to tell it:
Who: Who we are setting permissions for.
What: What change are we making? Are we adding or
removing the permission?
Which: Which of the permissions are we setting?

16
Users, files and manuals
Files
Changing Directory and File Permissions
Understanding The Permission Syntax
The "who" values we can use are:
u: User, meaning the owner of the file.
g: Group, meaning members of the group the file belongs to.
o: Others, meaning people not governed by the u and g
permissions.
a: All, meaning all of the above.
If none of these are used, chmod behaves as if "a" had been
used.

17
Users, files and manuals
Files
Changing Directory and File Permissions
Understanding The Permission Syntax
The "what" values we can use are:
"-": Minus sign. Removes the permission.
"+": Plus sign. Grants the permission. The permission is added
to the existing permissions.
"=": Equals sign. Set a permission and remove others.

18
Users, files and manuals
Files
Changing Directory and File Permissions
Understanding The Permission Syntax
The "which" values we can use are:
"r": The read permission.
"w": The write permission.
"x": The execute permission.
Example:
We want the user to have read and write permissions and the
group and other users to have read permissions only
$ chmod u=rw,og=r new_file.txt

19
Users, files and manuals
Files
Changing Directory and File Permissions
We can add the execute permission for everyone with the
following command:
$
We could have achieved the same thing without the "a" in the
"a+x" statement. The following command would have worked
just as well.
$
Let's say we want to remove the read permissions for the
"other" users from files that have a ".page" extension.
$

20
Users, files and manuals
Files
Changing Directory and File Permissions
We can add the execute permission for everyone with the
following command:
$ chmod a+x new_script.sh
We could have achieved the same thing without the "a" in the
"a+x" statement. The following command would have worked
just as well.
$ chmod +x new_script.sh
Let's say we want to remove the read permissions for the
"other" users from files that have a ".page" extension.
$ chmod o-r *.page

21
Users, files and manuals
Files
Changing Directory and File Permissions
As we can see, the read permission has been removed from the
".page" files for the "other" category of users. No other files
have been affected.
If we had wanted to include files in subdirectories, we could
have used the -R (recursive) option.
$ chmod -R o-r *.page

22
Users, files and manuals
Files
Changing Directory and File Permissions
The digits you can use and what they represent are listed
here:
0: (000) No permission.
1: (001) Execute permission.
2: (010) Write permission.
3: (011) Write and execute permissions.
4: (100) Read permission.
5: (101) Read and execute permissions.
6: (110) Read and write permissions.
7: (111) Read, write, and execute permissions.
23
Users, files and manuals
Files
Changing Directory and File Permissions
The digits you can use and what they represent are listed here:
rwx: Read, write and execute access permissions are all present.
rw- : Read and write permissions, no execution permissions.
r-x: Read and execute permissions, no write permissions.
-wx : No read permissions, write and execute permissions.
r-- : Only has the right to read.
-w- : Only has write permission.
--x : Only has execution rights.
--- : No access rights available.
24
Users, files and manuals
Files
Changing Directory and File Permissions
Some chmod examples:
$ chmod +r filename: we have given read(r) permission to the
test file.
$ chmod u=rw,go= filename: We gave read and write permissions
to the file owner. We have removed all access permissions for the
group and others.
$ chmod +x filename: We gave the file execution permission for
all users (user, group, other).
$ chmod +rw filename: We gave read and write permissions for
the user who gave the command, and only read permissions for
the group and others.

25
Users, files and manuals
Files
Changing Directory and File Permissions
Some chmod examples:
$ chmod 0 try: We removed all users' access permissions for the
file.
$ chmod 666 trial: We have given read and write permissions for
all users.
$ chmod 0755 or chmod 755 file_name: It does not matter if this
command is written in two different ways. Its function is the
same: it gives read, write and execution rights to the file owner,
while it also gives read and execution rights to the group and
others.
$ chmod ugo-rwx filename: We have removed all access
permissions of the file.
26
Users, files and manuals
Files
Changing Directory and File Permissions
Some chmod examples:
$ chmod 0 or chmod 000 also does the same thing.
$ chmod go-rw trial: We removed the read and write rights of the
group and other users on the file.
$ chmod –R u+r trial: We gave the user read permission for all
files and directories under the directory named trial. With the -R
(recursive) parameter, the chmod access permission of all files and
directories under the relevant directory is changed in the same
way.
$ chmod a+rwx and chmod 777 trial: Two different commands but
their functions are the same. It ensures that all users have read,
write and execute rights on the relevant file.
27
Users, files and manuals
Files
Changing Directory and File Permissions
Some chmod examples:
$ chmod go+r trial*: (*) Allows all files starting with trial with the
wildcard parameter to be read by the group and other users.
$ chmod go-rwx * or chmod 700 *: Used when inside a directory.
Read, write and execute access permissions for the group and
other users are removed for all files and subdirectories within the
directory where it is used.

28
Users, files and manuals
Files
Changing Directory and File Permissions
Let's add the read permission back on the ".page" files for the
others category of users. We must set the user and group
permissions as well, so we need to set them to what they are
already. These users already have read and write permissions,
which is 6 (110). We want the "others" to have read and
permissions, so they need to be set to 4 (100).
The following command will accomplish this:
$ chmod 664 *.page

29
Users, files and manuals
Files
When we use the "rm" command, we are actually removing
links. E.g.

UNIX> chmod 0644 f1


UNIX> rm f1
UNIX> ls -li f*
34778 -rw-r--r-- 2 plank 11 Sep 16 10:14 f2
34778 -rw-r--r-- 2 plank 11 Sep 16 10:14 f3
UNIX>

30
Users, files and manuals
Files
When the last link to a file is removed, then the file itself,
inode and all, is deleted. As long as there is a link pointing to a
file, however, the file remains. It is interesting to see what
happens when files with links are overwritten. For example,
suppose I do the following:
UNIX> cat > f2
This is now file f2
^D
UNIX> cat f2
This is now file f2
UNIX> cat f3
This
31 is now file f2
Users, files and manuals
Files
By saying you want to redirect output to the file f2, you end
up changing f3. This means that when the shell performs
output redirection, it opens the file and truncates it, instead of
removing the file and creating it anew.
Instead, suppose you do:
UNIX> gcc -o f2 ../Stat/src/ls1.c
UNIX> ls -li f*
34779 -rwxr-xr-x 1 plank 24576 Sep 16 10:16 f2
34778 -rw-r--r-- 1 plank 20 Sep 16 10:16 f3
UNIX>
You'll note that the c compiler gcc did a "rm f2" before
creating
32 f2 as an executable.
Users, files and manuals
Files
All directories have at least 2 links:
UNIX> mkdir test
UNIX> ls -li | grep test
34800 drwxr-xr-x 2 plank 512 Sep 16 10:17 test
UNIX>
This is because every directory contains two subdirectories
"." and ".." The first is a link to itself, and the second is a link to
the parent directory. Thus, there are two links to the directory
file "test": "test" and "test/."

33
Users, files and manuals
Files
Similarly, suppose we make a subdirectory of test:
UNIX> mkdir test/sub
UNIX> ls -li | grep test
34800 drwxr-xr-x 3 plank 512 Sep 16 10:17 test
UNIX>
Now there are three links to "test": "test", "test/.", and
"test/sub/.."

34
Users, files and manuals
Files
Besides these links which are automatically generated for you,
you cannot manually create links to directories. Instead, there is a
special kind of a link called a "symbolic link" (also called a "soft
link"), which you make using the command "ln -s". For example,
we can create a soft link to the test directory as follows:
UNIX> ln -s test test-soft
UNIX> ls -li | grep test
34800 drwxr-xr-x 3 plank 512 Sep 16 10:17 test
34801 lrwxrwxrwx 1 plank 4 Sep 16 10:18 test-soft -> test
UNIX>
Note that soft links have a different kind of directory listing. Moreover,
note that the generation of a soft link to "test" doesn't update the link
field
35
of test's inode. That only records regular, or "hard" links.
Users, files and manuals
Regex
• Regular Expression
• Regular expressions in computing enable the determination
of some strings in the text in a short and flexible way.
• These strings can be specific characters, words or
character patterns.
• Regular expressions are written using a formal language and
interpreted by a regular expression processor.
• A regular expression processor is a program that either
serves as a parser generator or examines the text and
identifies parts of it that fit the given description.

36
Users, files and manuals
Regex
•Global regular expression print (grep) is used to search for specific
values within text files:
$ grep "kelime" deneme_dosya
$ grep "kelime" . -R
$ grep "Kelime" deneme_dosya –i
•To be more specific, grep extracts parts from a given text within
certain criteria.
•Simply, grep asks you to enter a template, and then it searches for
texts that match this template in a text you specify.
•Lists all rows that match the specified template.
•Grep can be used alone or with pipes

37
Users, files and manuals
Regex
$ grep John /etc/passwd
#
$ grep -v John /etc/passwd
#
$ grep -c John /etc/passwd
#

$ grep -i John /etc/passwd


#
$ grep -r John /home/users
#

38
Users, files and manuals
Regex
$ grep John /etc/passwd
# Lists the lines containing the word John in the /etc/passwd file.
$ grep -v John /etc/passwd
# John in the /etc/passwd file will list the lines that are not passed.
$ grep -c John /etc/passwd
# It will list the number of times the word John in the /etc/passwd
file is used in the file.
$ grep -i John /etc/passwd
#case insensitivity will occur.
$ grep -r John /home/users
# will search the folder you specify and all its subfolders.

39
Users, files and manuals
Regex
Egrep:
Expressions ending with the ''+'' sign can be compared one or more
times.
Expressions ending with the sign ''-'' can be compared zero or once.
One or the other of the expressions separated by ''|'' or newline
character can be put into the comparison process.
Finally, this expression can be grouped by enclosing it in parentheses.

40
Users, files and manuals
Regex
Metacharacters
$ ‘ ’ “ ” ~ & | <<<>>> () {} * ? []\ .
File names that start with, contain or end with the above characters are
prohibited.
The reason is that Linux attributes a special meaning to each character
above within its own system.

41
Users, files and manuals
Regex - Metacharacters
'.': declares a single character for Regex
'*': zero or more preceding characters
'+': 1 or more of the characters before it
'?': either the character before it is present or absent "if statement"
'|': like a question mark but valid for the entire word group, not just one letter
'^': allows searching from the beginning of the line
'$': marks the end of the line
'\\': escape character - search for Metacharacters
'[]': select data in a string of characters
'[^]': hat in square brackets is 'not' like in programming languages

'()': Parentheses are used to control more than one data with Metacharacters

42
Users, files and manuals
Regex
$ grep "^[a-zA-Z]" pgm.s
#
$ grep -v "^#" pgm.s
#
$ grep -E "abc|xyz" file1
#
$ grep \\$ test2
#
$ ls -l *.ps | grep Sep
#

43
Users, files and manuals
Regex
$ grep "^[a-zA-Z]" pgm.s
#This displays every line in pgm.s whose first character is a letter.
$ grep -v "^#" pgm.s
#This displays every line in pgm.s whose first character is not a # (pound sign)
$ grep -E "abc|xyz" file1
#Display all lines in the file1 file that match either the abc or xyz string
$ grep \\$ test2
#Search for a $ (dollar sign) in the file named test2
$ ls -l *.ps | grep Sep
#Display files that end in “.ps” and were created in the month of September

44
Users, files and manuals
Regex
$ ls -l *.ps | grep Sep
#Display files that end in “.ps” and were created in the month of September
#The first part of this command line produces a list of files ending in .ps.
$ ls -l *.ps
-rw-r--r-- 1 user2 users 833233 Jun 29 16:22 buttons.ps
-rw-r--r-- 1 user2 users 39245 Sep 27 09:38 changes.ps
-rw-r--r-- 1 user2 users 608368 Mar 2 2000 clock.ps
-rw-r--r-- 1 user2 users 827114 Sep 13 16:49 commands.ps
$
#The second part of the command line pipes that list through grep, looking for
the pattern Sep
$ ls -l *.ps | grep Sep
-rw-r--r--
45
1 user2 users 39245 Sep 27 09:38 changes.ps
Users, files and manuals
Regex
To find a pattern that is more than one word long, enclose the string with
single or double quotation marks.
$ grep “Louisa May” extensions
Louisa May Alcott x74236
$ grep ar *
actors:Humphrey Bogart
alaska:Alaska is the largest state in the United States.
wilde:book. Books are well written or badly written.
$ ls
actors alaska hinterland tutors wilde
$ grep -v e *
actors:Mon Mar 14 10:00 PST 1936
wilde:That
46
is all.
Users, files and manuals
Regex - Metacharacters
$ ls a.c # ...
$ ls u..x # ...
$ ls ab*c # ...
$ ls ab?c # ...
$ ls ab|cd # ...
$ ls ^D.* # ...

$ ls .*d$ # ...
$ egrep "a\." BASH #...
$ ls [ab]c # ...
$ ls [^ab]c # ...
$ ls a(bc)* # ...

47
Users, files and manuals
Regex - Metacharacters
$ ls a.c # abc,adc,a&c etc..
$ ls u..x # unix, unax, ussx etc.
$ ls ab*c # ac, abc, abbc
$ ls ab?c # ac or abc
$ ls ab|cd # ab or cd
$ ls ^D.* # All files starting with D are returned. Here ^ represents the beginning of the
line. Asterix says that there will be more than one character before him. Since the
character before it is a dot, we can say that any char data can be included in the
characters that can come.

$ ls .*d$ # Files ending in d are returned. It is the same logic as above.


$ egrep "a\." BASH # 'a.' in Bash Containing expressions are returned. 'tolga.' like.
$ ls [ab]c # If there is a file named ac or bc, it does ls.
$ ls [^ab]c # Except for the ac and bc, all other data is returned. (cc,dc,fc etc..)
$ ls a(bc)* # a,abc,abcbc,abcbcbc etc..
48
References
• https://www.linode.com/docs/guides/linux-users-and-groups/

• https://www.howtogeek.com/437958/how-to-use-the-chmod-command-on-linux/

• https://www.ibm.com/docs/en/aix/7.3?topic=g-grep-command

• https://docs.oracle.com/cd/E19253-01/806-7612/filesearch-99633/index.html

49

You might also like