w3resource

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

Python Operating System Services: Exercise-8 with Solution

Write a Python program to find the parent’s process id, real user ID of the current process and change 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

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 Python skills with w3resource's quiz


Python: Tips of the Day

Python: Unknown Arguments Using *arguments

If your function can take in any number of arguments then add a * in front of the parameter name:

def myfunc(*arguments):
 for a in arguments:
   print a
myfunc(a)
myfunc(a,b)
myfunc(a,b,c)