Linux programming learning notes

Source: Internet
Author: User
Linux programming learning notes-general Linux technology-Linux programming and kernel information. The following is a detailed description. I have had enough time in my spare time recently. In addition, I have two Linux programming books on hand. I hope I can go to the room early.

1. First Program: hello. c
Haha, it seems that no matter which programming method you learn, it's from Hello, World! Started!
Int main (int argc, char ** argv)
{
Printf ("Hello World! \ N ");
Return 0;
}
To compile this program, we only need to execute it in the command line:
Gcc-o hello. c
The gcc compiler will generate a hello executable file for us. Execute./hello to see the output result of the program. In the command line, gcc indicates that we use gcc to compile our source program. The-o option indicates that we require the compiler.

The executable file output for us is "hello" and "hello. c" is our source program file. In addition, the-c option indicates that only the compiler is required to output the target code, and the executable file is not required.

2. The simplest Makefile file
For example, the content of Makefile is as follows:
Hello: hello. o
Gcc-o hello. o
Hello. o: hello. c
Gcc-c hello. c
Clean:
Rm-f *. o

The first line indicates dependency, and the second line indicates rules.
Hello: hello. o
The dependent object (components) of target hello is hello. o.
When the dependent object is modified after the target is modified, it is necessary to execute the command specified by the rule line.
Note that the colon in a row of the rule is followed by a TAB key.

3. Several useful system functions
Pid_t getpid (void); // obtain the process ID.
Pid_t getppid (void); // obtain the ID of the parent process (the process that creates the process that calls this function)
Uid_t getuid (void); // obtain the ID of the process owner.
Uid_t geteuid (void); // obtain the valid user ID of the process.
Gid_t getgid (void); // obtain the group ID.
Git_t getegid (void); // get the valid group ID
Getpwuid (uid_t); // get more user details

Use the above functions
# Include
# Include
# Include

Int main ()
{
Pid_t my_pid, parent_pid;
Uid_t my_uid, my_euid;
Gid_t my_gid, my_egid;
Struct passwd * my_info;

My_pid = getpid ();
Parent_pid = getppid ();
My_uid = getuid ();
My_euid = geteuid ();
My_gid = getgid ();
My_egid = getegid ();
My_info = getpwuid (my_uid );

Printf ("Process ID: % ld \ n", my_pid );
Printf ("Parent ID: % ld \ n", parent_pid );
Printf ("User ID: % ld \ n", my_uid );
Printf ("valid tive User ID: % ld \ n", my_euid );
Printf ("Group ID: % ld \ n", my_gid );
Printf ("valid tive Group ID: % ld \ n", my_egid );

If (my_info)
{
Printf ("My Login Name: % s \ n", my_info-> pw_name );
Printf ("My Password: % s \ n", my_info-> pw_passwd );
Printf ("My User ID: % ld \ n", my_info-> pw_uid );
Printf ("My Group ID: % ld \ n", my_info-> pw_gid );
/* Printf ("My RealName: % s \ n", my_info-> gecos );*/
Printf ("My Home Dir: % s \ n", my_info-> pw_dir );
/* Printf ("My Work Shell: % s \ n", my_info-> shell );*/
}

Return 0;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.