Linux Programming PDF
Document Details
Uploaded by CheerfulCatSEye3203
Tags
Related
- Desislav Andreev, Stanimir Lukanov - C++ Programming for Linux Systems_ Create robust enterprise software for Linux and Unix-based operating systems-Packt Publishing (2023)-1-364-375.pdf
- Lecture 03 - Threads PDF
- Operating System Concepts (9th Edition 2012) PDF
- Linux Kernel Programming PDF
- Computer Knowledge PDF
- Systemy Operacyjne Exam Paper PDF
Summary
This document provides an introduction to Linux programming, focusing on operating systems and server computers. It also touches upon Linux architecture and installation, making it suitable for undergraduate-level study.
Full Transcript
Linux Programming FYCS SEMESTER-II 2 Operating System OS is a platform between hardware & software of your computer. Intelligence Output Softwar e Platform...
Linux Programming FYCS SEMESTER-II 2 Operating System OS is a platform between hardware & software of your computer. Intelligence Output Softwar e Platform OS Hardware Processing Power 3 Operating System OS is divided into 2 parts: Instructions are passed to User kernel for execution. User space Output is returned back to Kernel the user space Kernel is an engine of OS. Engine Kernel provides Performance Reliability Security Stability To an OS. 4 5 Server Computers Runs 24/7 & 365 days a year. Redundant, fault-tolerant, highly available & hot swappable. Server needs powerful hardware. For maximum utilization of server hardware, powerful server OS is required. Windows is client OS whereas LINUX is server OS. Client OS is in GUI form but server OS is always in command line format. 6 Syllabus: Unit 1 7 Introduction Operating system concepts, Server & Client operating system, Features of Server computers, Introduction of Linux, Linux Terminology, Distributions, Linux kernel v/s distributions. Why learn Linux? Importance of Linux in the software ecosystem: web servers, supercomputers, mobile and servers. Introduction to Red Hat Enterprise Linux 7 Installation Installation methods, Hands on Installation using CD/DVD or USB drive. Linux Structure Linux Architecture, Linux Filesystem, The boot process, init scripts, runlevels, Shutdown process, Very basic introductions to Linux processes, Packaging methods: rpm/deb, Graphical Vs Command line. Unit 1 9 Linux Linux is Free and Open Source. Linux is similar to UNIX but not one of the Unix OS. Linux is a kernel. Linux kernel is written in C. Linus Torvalds of open source community launched Linux kernel in 1994. Linux was released under GNU/GPL (Grand Non-Unix / General Public License). Open source community has developed kernel of Linux. Different companies have developed their own user space to make complete OS. 10 Linux Distributions / Flavours of Linux Redhat Suse Debian / Ubantu Fedora Mandriva Slackware Mandrake CentOS Introduction to Red Hat Enterprise Linux 11 In 1993, Marc Ewing and Bob Young founded Red Hat, the first Linux distributor operating as a business. Since then, Red Hat has acquired other companies to integrate specific Linux- related technologies. Red Hat went public in 1999, thus becoming the first Linux-based company. Today Linux in general and Red Hat Linux in particular is at the heart of the IT organization in many companies. Large parts of the Internet operate on Linux, using popular applications such as the Apache web server or the Squid proxy server. Stock exchanges use Linux in their real-time calculation systems, and large Linux servers are running essential business applications on top of Oracle and SAP. Linux has largely replaced UNIX, and Red Hat is a leading force in Linux. One reason why Red Hat has been so successful since the beginning is the level of support the company provides. Red Hat offers three types of support, and this gives companies the confidence they need to run vital business applications on Linux. The three types of Linux support provided by Red Hat are as follows: Hardware Support Red Hat has agreements with every major server hardware vendor to make sure that whatever server a customer buys, the hardware vendor will assist them in fixing hardware issues, when Red Hat is installed on it. Software Support Red Hat has agreements with every major enterprise software vendor to make sure that their software runs properly on top of the Red Hat Linux operating system and that the enterprise software is also guaranteed to run on Red Hat Linux by the vendor of the operating system. Hands-on Support This means that if a customer is experiencing problems accomplishing tasks with Red Hat software, the Red Hat Global Support organization is there to help them by fixing bugs and providing technical assistance. Red Hat 12 Red Hat Linux RHL-3,..., RHL-9 (2.4) Red Hat RHEL Fedora (Free (Commercial) & Open (2.6) Enterprise Source) Linux RHEL - 3 (2.6) RHEL - 4 (2.6) RHEL - 5 (2.6) RHEL - 6 (2.6) RHEL - 7 (3.10) RHEL – 8 (4.18) RHEL – 9 (5.14) 13 Red Hat Linux Kernel Kernel Version is always an even number like 2.2, 2.4, 2.6, 3.10 etc. Major no. 2. 0. Minor no How do we access storage devices in Linux? 14 Storage devices are physical devices for e.g. hard Mount drives Storage OS Point They cannot be accessed physically. We can access them logically. OS provides mount point to access storage devices. F:\ C:\ 300GB 200GB In Windows OS, drive letters like C:\ , D:\ are 1 mount points. TB Mount points provide access to storage. 200GB 300GB D:\ E:\ How do we access storage devices on 15 Linux? Linux does not use drive letters. In Linux storage can be accessed through folders(directories). Applications Song 200GB 200GB s In the diagram, songs and movies are the mount points to access 200gb and 300gb 1 storage space respectively. TB 300GB 300GB In Linux, when we connect pen drive, we will create a folder called usb. This folder Movies Work will act as a mount point to pen drive. Now we can access pen drive through folder usb. All folders are not mount points but every mount point is a folder. 16 OS from Hard drive’s point of view..? Initially hard drive was empty. When OS is installed, system information gets stored in hard drive. From hard drive point of view, OS is a collection of files and folders containing system information. Windows: C:\Windows\ … OS Root C:\Program Files\ …. Installed soft wares (origin ) C:\Users\ …. User account / boot Directory structure starts from C: home C is the mount point of directory partition. Linux: dev Directory structure starts from “ / “ (slash). usr “ / “ is mount point of directory partition. etc lib 17 OS from Hard drive’s point of view..? Windows file system starts from C, whereas Linux file system starts from /. Entire windows os is installed on a single partition. Every directory in a linux can become mount point of a partition. We can spread os onto multiple partitions by making every system directory a mount point to different partition. 18 Our scenario swap 4 GB /boot 30 GB Hard Drive 500 MB 2 GB RAM 30 500 MB - /boot 4 GB GB 20 GB 20 GB - / /home 4 GB - /home / 4 GB – swap Remaining space in hard drive can be used by user for data storage similar to D,E,F drives of Windows. 19 Virtual Memory Space borrowed by RAM from Hard Drive to balance the overload of the processes Swap is a partition dedicated to work as virtual memory for RAM. Size of swap should be double than that of RAM. Basics of Linux: 20 Core OS: GUI – input using pointer (desktop) CLI – input using cursor (command line) Terminal : Login screen GUI and CLI Provides multiple terminals: Total 6 terminals If graphics is ON – 1 GUI & 5 CLI GUI – F1 Ctrl + Alt + F1 – GUI CLIs – F2-F6 Ctrl + Alt + F2-F6 – CLI Multi User OS: multiple users can login simultaneously or single user can have multiple login instances Command: it is the name of a program that we want to run Shell: To communicate commands to the operating system kernel, an interface is needed that sits between the kernel and the end user issuing these commands. This interface is known as the shell. Several shells are available on RHEL. tcsh A shell with a scripting language that works like the C programming language. It is very popular with C programmers. zsh A shell that is compatible with Bash but offers even more features. sash This stands for stand-alone shell. This is a minimal-feature shell that runs in almost all environments. Therefore, it is very well suited for system troubleshooting. SH shell KSH korn shell Bash (short for the Bourne Again Shell) is the default shell in Red Hat linux. Most advanced & user friendly. Features of bash shell: auto complete of commands & syntax arguments, history of commands is stored for every user, compact syntax 21 Linux Architecture The Linux system works basically on 4 layers. The diagram below shows the architecture of a Linux System. Hardware − Hardware consists of all physical devices attached to the System. For example: Hard disk drive, RAM, Motherboard, CPU etc. Kernel − Kernel is the core component for any (Linux) operating system which directly interacts with the hardware. Shell − Shell is the command interpreter. Shell is the interface which takes input from Users and sends instructions to the Kernel, Also takes the output from Kernel and send the result back to output shell. Applications − These are the utility programs which runs on Shell. This can be any application like Your web browser, media player, text editor etc. Linux Architecture 22 Linux File System / Linux Directory 23 Structure / usr boot dev etc home lib media mnt opt proc root tmp var bin sbin In linux directory structure starts from /. /: / is a root directory. / is a mount point of directory system. All other files and directories are inside / directory. /boot: Where boot loader and boot files are located. /dev: contains device drivers needed by hardware devices. OS cannot use hardware directly. Device driver is a link between OS and hardware. Print my Prin Prin User file OS t Drivert Printer (o/p) /home: contains home directory of all local users. Every user will have a personal folder to put his files with his name like /home/tom. /home student tom jerry donald Linux File System: Various system 24 directories of Linux /root: The home folder for the root user. /proc: Mount point of RAM, this directory is for processes running on the system, and you can access them and see info about the current processes. /tmp: Contains the temporary files. This directory is shared by all users. Public directory. /var: Contains system logs and other variable data. /etc: Contains configurations of all applications. /usr: Where commands are stored. Commands in linux are of 2 types binary and superbinary. Contains bin and sbin directories. /usr/bin: Where binaries of commands are located used by all users. /usr/sbin: binaries here are for root user only. Linux File System: Various system 25 directories of Linux /lib: Where the libraries of the installed packages located since libraries shared among all packages. Kernel modules. /media: Empty. Here is the external devices like DVDs and USB can be mounted and you can access their files from here. /mnt: Empty. Where you mount external devices USB or DVD. /opt: Some optional packages are located here and this is managed by the package manager. 26 Absolute path Linux is a case and space sensitive OS. cd command is used to change directory . current directory .. previous directory ~ home directory Absolute path starts from current location up to destination. 27 28 Absolute path 1. cd desktop …….wrong cd Desktop …….. right 2. cd conf ……. Wrong cd /etc/httpd/conf ……. Right 3. cd /usr/bin 4. cd /usr …..right cd.. ……. Best 5. cd /local/sbin ……….. Wrong cd local/sbin …………right 6. cd ~ Absolute path will not always starts with /. If path starts with / then that / is / directory. 29 Boot Process 29 When a machine is powered ON, it will start TWO processes. 1st START process it will start hardware 2nd BOOT process it will start OS Sector 1 SMPS 1 SECTOR = 512 BYTES CMOS & BIOS CHIP ROM MBR MP 446 BYTES 64 BYTES 2 BYTES BOOT SECTOR PARTITION MAGIC BOOT LOADER TABLE NUMBER (GRUB) 512 BYTES 30 Boot Process SMPS – ELECTRICAL SIGNAL - MOTHERBOARD – PROCESSOR - BIOS BIOS (BASIC INPUT OUTPUT SYSTEM) PROGRAM RUNS ITSELF – INT 19H – POST (POWER ON SELF TEST) POST DETECTS CONNECTED HARDWARE – CREATES THEIR LIST BIOS WILL SEARCH FOR BOOTABLE DEVICE – THERE CAN BE MULTIPLE BOOTABLE DEVICES – BOOT SEQUENCE CMOS CMOS 1ST BOOTABLE DEVICE IS HARD DRIVE BIOS - INT 13H – GO TO THE HARD DRIVE – TO GET OS FOR BIOS – HARD DRIVE CYLINDERS, HEADS & SECTORS (CHS) 13H -> 1ST SECTOR – MBR (MASTER BOOT RECORD) – BRINGS FIRST FILE FOUND ON RAM FIRST FILE BOOT LOADER – EXECUTES ITSELF CHS OF KERNEL 13H WILL GO THE GIVEN CHS – BRING KERNEL ON RAM KERNEL WILL EXECUTE ITSELF – WILL TAKE OVER THE BOOT PROCESS BIOS’s TASK IS DONE – KERNEL WILL START BOOTING THE OS 31 Runlevels A runlevel is a bundle of service. Runlevel instructs the OS what services to run and what not to run. RHEL 7 can be booted into one of a number of runlevels. There are total 7 runlevels through 0 to 6 except 4 in RHEL 7. Runlevel 0 - The halt runlevel. This is the runlevel at which the system shuts down. Runlevel 1 – Causes the system to start up in a single user mode under which only the root user can log in. In this mode the system does not start any networking, graphics or multi-user services. This run level is ideal for system administrators to perform system maintenance or repair activities. Runlevel 2 - Boots the system into a multi-user mode with command line. This runlevel does not, however, start the network. No GUI. Runlevel 3 - Similar to runlevel 2 except that networking services are started. This is the most common runlevel for server based systems that do not require any kind of graphical desktop environment. 32 Runlevels Runlevel 4 - Undefined runlevel. Runlevel 5 - Boots the system into a networked, multi-user state with X Window System capability. By default the graphical desktop environment will start at the end of the boot process. This is the most common run level for desktop or workstation use. Runlevel 6 - Reboots the system. To check current run level: # runlevel it will show two entries. 1st is previous runlevel and 2nd is current runlevel. To change system runlevel: # init For example: # init 3 Shutdown Process 33 The shutdown command in Linux is used to shut down the system in a safe way. You can shut down the machine immediately, or schedule a shutdown using 24 hour format. It brings the system down in a secure way. When the shutdown is initiated, all logged-in users and processes are notified that the system is going down, and no further logins are allowed. Only root user can execute shutdown command. Syntax of shutdown Command: # shutdown [OPTIONS] [TIME] [MESSAGE] options – Shutdown options such as halt, power-off (the default option) or reboot the system. time – The time argument specifies when to perform the shutdown process. message – The message argument specifies a message which will be broadcast to all users. Options -r: reboot after shutdown -h/H: halt -c: Cancels a running shutdown. -k: Only send out the warning messages and disable logins, do not actually bring the system down. -P: Poweroff Shutdown Process 34 How to use shutdown In it’s simplest form when used without any argument, shutdown will power off the machine. #shutdown How to shutdown the system at a specified time The time argument can have two different formats. It can be an absolute time in the format hh:mm and relative time in the format +m where m is the number of minutes from now. The following example will schedule a system shutdown at 05 A.M: #shutdown 05:00 The following example will schedule a system shutdown in 20 minutes from now: #shutdown +20 How to shutdown the system immediately To shutdown your system immediately you can use now: #shutdown now How to broadcast a custom message The following command will shut down the system in 10 minutes from now and notify the users with message “System upgrade”: #shutdown +10 "System upgrade" Shutdown Process 35 How to halt your system This can be achieved using the -H option. #shutdown -h Halt instructs the hardware to stop all CPU functions, but leaves it powered on. You can use it to get the system to a state where you can perform low level maintenance. How to make shutdown power-off machine Although this is by default, you can still use the -P option to explicitly specify that you want shutdown to power off the system. #shutdown -P How to reboot using shutdown For reboot, the option is -r. #shutdown -r You can also specify a time argument and a custom message: #shutdown -r +5 "Updating Your System" The command above will reboot the system after 5 minutes and broadcast Updating Your System” How to cancel a scheduled shutdown If you have scheduled a shutdown and you want to cancel it you can use the -c argument: #shutdown -c When canceling a scheduled shutdown, you cannot specify a time argument, but you can still broadcast a message that will be sent to all users. #shutdown -c "Canceling the reboot"