w3resource

Python: Find the parent’s process id, real user ID of the current process and change real user ID


8. Process and User ID Operations

Write a Python program to find the parent's process ID, real user ID of the current process and change the real user ID.

Sample Solution:

Python Code :

import os
print("Parent’s process id:",os.getppid())
uid = os.getuid()
print("\nUser ID of the current process:", uid)
uid = 1400
os.setuid(uid)
print("\nUser ID changed")
print("User ID of the current process:", os.getuid())

Sample Output:

Parent’s process id: 11069

User ID of the current process: 1000
Traceback (most recent call last):
  File "/tmp/sessions/caaafe2b92f1fb7d/main.py", line 6, in 
    os.setuid(uid)
PermissionError: [Errno 1] Operation not permitted

For more Practice: Solve these Related Problems:

  • Write a Python program to retrieve and print the parent process ID and the real user ID of the current process using os.getppid() and os.getuid().
  • Write a Python script to change the real user ID of the current process (if permissions allow) and then verify the change by printing the new user ID.
  • Write a Python function that logs the current process ID, parent process ID, and real user ID, and then simulates a change in privileges.
  • Write a Python program to compare the real and effective user IDs of the current process and print a message indicating if they differ.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to create a file and write some text and rename the file name.
Next: Write a Python program to retrieve the current working directory and change the dir (moving up one).

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.