Linux Memory Usage

The objective of the Linux Memory Usage CTF challenge is to efficiently manage and process memory usage data for different processes based on given inputs.

Linux Memory Usage - CTF Challenge Writeup

Challenge Information

  • Name: Linux Memory Usage
  • Category: PPC / Leet Code
  • Objective: The objective of the “Linux Memory Usage” CTF challenge is to efficiently manage and process memory usage data for different processes based on given inputs.

Solution

Encountering a Leet Code challenge within a CTF was an interesting experience. Here’s how I tackled it:

  1. Understanding Inputs:

    • This was my first encounter with a Leet Code style challenge in a CTF context, very very fun.
    • The problem consisted of three types of inputs:
      1. Variables N and Q: Representing the number of processes and queries.
      2. Processes’ Data: Including process id, parent process id, and memory usage.
      3. Queries: Containing necessary information for further analysis.
  2. Memory Mapping:

    • Utilizing the received inputs, I structured the memory by creating a dictionary.
    • The dictionary’s key-value pairs were organized to represent parent processes and their corresponding children processes, facilitating efficient data retrieval.
  3. Handling Queries:

    • Processing queries involved reading and parsing the data structure I coded to provide the desired output efficiently.

      Flag

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      
      from collections import defaultdict
      
      def calculate_memory_usage(p, q):
          children = defaultdict(list)
          memory = {}
      
          for a, b, c in p:
              memory[a] = c
              if b != 0:
                  children[b].append(a)
      
          memo = {}
          results = []
      
          for query in q:
              stack = [query]
              total_memory = 0
      
              while stack:
                  current_pid = stack.pop()
                  if current_pid in memo:
                      total_memory += memo[current_pid]
                      continue
      
                  total_memory += memory[current_pid]
                  children_list = children[current_pid]
      
                  for child in children_list:
                      stack.append(child)
      
              results.append(total_memory)
              memo[query] = total_memory
      
          return results
      
      N, Q = map(int, input().split())
      p = [list(map(int, input().split())) for _ in range(N)]
      q = [int(input()) for _ in range(Q)]
      
      results = calculate_memory_usage(p, q)
      for result in results:
          print(result)
      

Flag

The flag for this challenge is: wgmy{XXXXXXXXXX}.

This writeup demonstrates the process of efficiently managing and processing memory usage data in the “Linux Memory Usage” CTF challenge using appropriate data structures and systematic query handling. For any further inquiries or clarifications, feel free to ask.

Last updated on Dec 17, 2023 00:00 UTC